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

Random gives different values when range passed as float #2612

Closed
sudeeptarlekar opened this issue Mar 3, 2022 · 5 comments
Closed

Random gives different values when range passed as float #2612

sudeeptarlekar opened this issue Mar 3, 2022 · 5 comments
Assignees
Labels
Milestone

Comments

@sudeeptarlekar
Copy link

Facing few issues in truffleruby and random behavior is not same in MRI and truffle

# Ruby 2.7.0
=> random = Random.new(12_345)
=> random.rand(0...100)
98
=> random.rand(0...100)
29
=> random.rand(0...100)
1

# truffleruby-head
=> random = Random.new(12_345)
=> random.rand(0...100)
98
=> random.rand(0...100)
29
=> random.rand(0...100)
1

When max range is passed as 0.0...100.0 or 0.0...100 observed strange behaviour

# Ruby 2.7.0
=> random = Random.new(12_345)
=> random.rand(0.0...100.0)
92.96160928171479
=> random.rand(0.0...100.0)
31.63755545817859
=> random.rand(0.0...100.0)
18.391881167709446

# truffleruby-head
=> random = Random.new(12_345)
=> random.rand(0.0...100.0)
0.0
=> random.rand(0.0...100.0)
0.0
=> random.rand(0.0...100.0)
0.0

Is this expected or am I missing something here?

Feel free to close if this is irrelevant or duplicate. Thanks in advance

@eregon eregon added the bug label Mar 3, 2022
@eregon
Copy link
Member

eregon commented Mar 3, 2022

This is a bug, thank you for the report.
It seems the issue is in the Random#rand(float1...float2) case, which was probably missed due to being rarely used and probably not well tested.

@eregon eregon added this to the 22.1.0 milestone Mar 3, 2022
@sudeeptarlekar
Copy link
Author

Also observed

=> [].sample(random: Random.new(12_345))

<internal:core> core/truffle/random_operations.rb:51:in `invalid_argument': invalid argument - negative limit: 0 (ArgumentError)
	from <internal:core> core/truffle/random_operations.rb:55:in `rand_int'
	from <internal:core> core/truffle/random_operations.rb:24:in `random'
	from <internal:core> core/random.rb:98:in `rand'
	from <internal:core> core/array.rb:1018:in `rand'
	from <internal:core> core/array.rb:1053:in `sample'
	from (irb):1:in `<top (required)>'
	from <internal:core> core/kernel.rb:407:in `loop'
	from <internal:core> core/throw_catch.rb:36:in `catch'
	from <internal:core> core/throw_catch.rb:36:in `catch'
	from /home/starlekar/.rbenv/versions/truffleruby-head/lib/gems/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
	from <internal:core> core/kernel.rb:376:in `load'
	from <internal:core> core/kernel.rb:376:in `load'
	from /home/starlekar/.rbenv/versions/truffleruby-head/bin/irb:42:in `<main>'

@eregon
Copy link
Member

eregon commented Mar 8, 2022

rand(Float Range) is fixed by 4036807.
The sample case has a separate upcoming fix.

@sudeeptarlekar
Copy link
Author

Thanks @eregon, @bjfish for fixing this

@bjfish
Copy link
Contributor

bjfish commented Mar 8, 2022

The Array#sample issue is fixed at 25398cf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants