Skip to content

Commit

Permalink
Fix Random#rand(Range(Float, Float)) to return Float (#6445)
Browse files Browse the repository at this point in the history
* Fix Random#rand(Range(Float, Float)) to return Float

* fixup! Fix Random#rand(Range(Float, Float)) to return Float
  • Loading branch information
straight-shoota authored and RX14 committed Jul 29, 2018
1 parent 665b18c commit bae4086
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions spec/std/random_spec.cr
Expand Up @@ -91,12 +91,21 @@ describe "Random" do
x = rand(1.8..3.2)
x.should be >= 1.8
x.should be <= 3.2

rand(1.0_f32..1.0_f32).should eq(1.0_f32)
x = rand(1.8_f32..3.2_f32)
x.should be >= 1.8_f32
x.should be <= 3.2_f32
end

it "does with exclusive range of floats" do
x = rand(1.8...3.3)
x.should be >= 1.8
x.should be < 3.3

x = rand(1.8_f32...3.3_f32)
x.should be >= 1.8_f32
x.should be < 3.3_f32
end

it "raises on invalid range" do
Expand Down
4 changes: 2 additions & 2 deletions src/random.cr
Expand Up @@ -273,12 +273,12 @@ module Random
rand_range(range)
end

# Returns a random `Float64` in the given *range*.
# Returns a random `Float` in the given *range*.
#
# ```
# Random.new.rand(6.2..21.768) # => 15.2989
# ```
def rand(range : Range(Float, Float)) : Float64
def rand(range : Range(Float, Float)) : Float
span = range.end - range.begin
if range.excludes_end?
unless range.begin < range.end
Expand Down

0 comments on commit bae4086

Please sign in to comment.