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

Fix Random#rand(Range(Float, Float)) to return Float #6445

Merged
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
9 changes: 9 additions & 0 deletions spec/std/random_spec.cr
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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*.
#
# ```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a doc update too (2 lines above).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks.

# 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