Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Random with range #1875

Merged
merged 2 commits into from

3 participants

Piotr Niełacny Don't Add Me To Your Organization a.k.a The Travis Bot Dirkjan Bussink
Piotr Niełacny

MRI:

rand(0.25..0.75) #=> 0.6428937309734553 

Rubinius before patch:

rand(0.25..0.75)
ArgumentError: invalid argument - 0.5
    from kernel/common/random19.rb:19:in `random'
    from kernel/common/random19.rb:8:in `random_range'
    from kernel/common/kernel19.rb:51:in `rand'
    from (irb):1
    from kernel/common/block_environment.rb:75:in `call_on_instance'
    from kernel/common/eval.rb:75:in `eval'
    from kernel/common/kernel19.rb:42:in `loop'
    from kernel/common/throw_catch19.rb:8:in `catch'
    from kernel/common/throw_catch.rb:10:in `register'
    from kernel/common/throw_catch19.rb:7:in `catch'
    from kernel/common/throw_catch19.rb:8:in `catch'
    from kernel/common/throw_catch.rb:10:in `register'
    from kernel/common/throw_catch19.rb:7:in `catch'
    from kernel/delta/codeloader.rb:68:in `load_script'

Rubinius after patch

rand(0.25..0.75) # => 0.6086817891310674 
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request fails (merged b3503128 into dd510a8).

Dirkjan Bussink
Owner

Did you actually run all the specs? Looks like this change breaks the build

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request fails (merged ecded34f into dd510a8).

Piotr Niełacny

Looks like this change breaks the build

Fixed. Now travis return

Error: #<NativeException: org.virtualbox_4_1.VBoxException: The function "powerDown" returned an error condition: "The virtual machine is being powered down"  (0x80bb0002)>
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 01b799d2 into 9bb0a65).

kernel/common/random19.rb
@@ -16,7 +16,7 @@ def random(limit)
random_range(limit)
else
limit_int = Rubinius::Type.coerce_to limit, Integer, :to_int
- raise ArgumentError, "invalid argument - #{limit}" if limit_int <= 0
+ raise ArgumentError, "invalid argument - #{limit}" if limit_int <= 0 && limit <= 0
Dirkjan Bussink Owner

Isn't the first check now duplicate since if the last one matches, the first one has to match too by definition?

Piotr Niełacny
LTe added a note

I agree. But with this change I need also little modify one spec. Check out new version :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 46cccc6f into c32cafe).

Dirkjan Bussink
Owner

Does this spec change still make it pass on MRI too?

Piotr Niełacny

Does this spec change still make it pass on MRI too?

No, because MRI does not execute <= method

Dirkjan Bussink
Owner

Then the spec is problematic and we can't spec it like this. I think we need to explicitly check here for a float type. You should probably check how MRI does this exact coercion here so we can match the behavior.

Piotr Niełacny

MRI check each type and try generate random number from each type (click)

  • if not float, try to_int and generate rand
  • if float -> generate rand
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged dc43f492 into c32cafe).

Dirkjan Bussink
Owner

Much better!

Dirkjan Bussink dbussink merged commit 8cf8366 into from
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 5e927f6 into c32cafe).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 0 deletions.
  1. +3 −0  kernel/common/random19.rb
  2. +6 −0 spec/ruby/core/kernel/rand_spec.rb
3  kernel/common/random19.rb
View
@@ -14,6 +14,9 @@ def random(limit)
else
if limit.kind_of?(Range)
random_range(limit)
+ elsif limit.kind_of?(Float)
+ raise ArgumentError, "invalid argument - #{limit}" if limit <= 0
+ random_float * limit
else
limit_int = Rubinius::Type.coerce_to limit, Integer, :to_int
raise ArgumentError, "invalid argument - #{limit}" if limit_int <= 0
6 spec/ruby/core/kernel/rand_spec.rb
View
@@ -59,6 +59,12 @@
rand l
end
end
+
+ ruby_version_is "1.9" do
+ it "returns a float for an range argument where max is < 1" do
+ rand(0.25..0.75).should be_kind_of(Float)
+ end
+ end
end
describe "Kernel#rand" do
Something went wrong with that request. Please try again.