Skip to content

Commit

Permalink
Merge pull request #6686 from oprypin/rand0
Browse files Browse the repository at this point in the history
Revert "allow rand from zero value"
  • Loading branch information
ysbaddaden authored Sep 10, 2018
2 parents 4a4ff9c + bb57c60 commit 2eb4718
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
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

0 comments on commit 2eb4718

Please sign in to comment.