Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "allow rand from zero value" #6686

Merged
merged 1 commit into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions spec/std/random_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ describe "Random" do
5.times do
rand(Int64::MAX).should be >= 0
end

rand(0).should eq 0
end

it "float number" do
Expand All @@ -52,17 +50,19 @@ describe "Random" do
x.should be < 3.5
end

it "float number 0.0" do
rand(0.0).should eq 0.0
end

it "raises on invalid number" do
expect_raises ArgumentError, "Invalid bound for rand: 0" do
rand(0)
end
expect_raises ArgumentError, "Invalid bound for rand: -1" do
rand(-1)
end
end

it "raises on invalid float number" do
expect_raises ArgumentError, "Invalid bound for rand: 0.0" do
rand(0.0)
end
expect_raises ArgumentError, "Invalid bound for rand: -1.0" do
rand(-1.0)
end
Expand Down
6 changes: 1 addition & 5 deletions src/random.cr
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ module Random
{% utype = "UInt#{size}".id %}
{% for type in ["Int#{size}".id, utype] %}
private def rand_int(max : {{type}}) : {{type}}
if max == 0
return {{type}}.new(0)
end

unless max > 0
raise ArgumentError.new "Invalid bound for rand: #{max}"
end
Expand Down Expand Up @@ -254,7 +250,7 @@ module Random
# Random.new.rand(10.725) # => 7.70147
# ```
def rand(max : Float) : Float64
unless max >= 0
unless max > 0
raise ArgumentError.new "Invalid bound for rand: #{max}"
end
max_prec = 1u64 << 53 # Float64, excluding mantissa, has 2^53 values
Expand Down