diff options
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | random.c | 10 | ||||
-rw-r--r-- | test/ruby/test_rand.rb | 3 |
3 files changed, 16 insertions, 3 deletions
@@ -1,3 +1,9 @@ +Wed May 26 12:08:06 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * random.c (random_rand): subtraction method of non-numeric can + return Float, and add the result of random to the beginning of + range, not the opposite. [ruby-dev:41410] + Wed May 26 10:35:37 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> * configure.in (rb_cv_large_fd_select): needed on mingw, even @@ -1077,9 +1077,13 @@ random_rand(int argc, VALUE *argv, VALUE obj) switch (TYPE(v)) { case T_BIGNUM: return rb_big_plus(v, beg); - case T_FLOAT: - RFLOAT_VALUE(v) += RFLOAT_VALUE(rb_check_to_float(beg)); - return v; + case T_FLOAT: { + VALUE f = rb_check_to_float(beg); + if (!NIL_P(f)) { + RFLOAT_VALUE(v) += RFLOAT_VALUE(f); + return v; + } + } default: return rb_funcall2(v, id_plus, 1, &beg); } diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb index b4adc8eaf5..ed74c5b211 100644 --- a/test/ruby/test_rand.rb +++ b/test/ruby/test_rand.rb @@ -357,6 +357,9 @@ END v = r.rand(3.1..4) assert_instance_of(Float, v, '[ruby-core:24679]') assert_includes(3.1..4, v) + + now = Time.now + assert_equal(now, r.rand(now..now)) end def test_random_float |