diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | random.c | 8 | ||||
-rw-r--r-- | test/ruby/test_rand.rb | 7 |
3 files changed, 18 insertions, 2 deletions
@@ -1,3 +1,8 @@ +Thu Apr 8 07:22:05 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * random.c (rand_init): ignore higher bits if all they are same as + the lower sign bit. [ruby-core:29292](2) + Thu Apr 8 07:16:19 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> * lib/irb/cmd/help.rb (IRB::ExtendCommand::Help#execute): use RI @@ -367,6 +367,7 @@ rand_init(struct MT *mt, VALUE vseed) { volatile VALUE seed; long blen = 0; + long fixnum_seed; int i, j, len; unsigned int buf0[SIZEOF_LONG / SIZEOF_INT32 * 4], *buf = buf0; @@ -374,9 +375,12 @@ rand_init(struct MT *mt, VALUE vseed) switch (TYPE(seed)) { case T_FIXNUM: len = 1; - buf[0] = (unsigned int)(FIX2ULONG(seed) & 0xffffffff); + fixnum_seed = FIX2LONG(seed); + buf[0] = (unsigned int)(fixnum_seed & 0xffffffff); #if SIZEOF_LONG > SIZEOF_INT32 - if ((buf[1] = (unsigned int)(FIX2ULONG(seed) >> 32)) != 0) ++len; + if ((long)(int)fixnum_seed != fixnum_seed) { + if ((buf[1] = (unsigned int)(fixnum_seed >> 32)) != 0) ++len; + } #endif break; case T_BIGNUM: diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb index 157f542dea..25c07d3288 100644 --- a/test/ruby/test_rand.rb +++ b/test/ruby/test_rand.rb @@ -398,4 +398,11 @@ END assert(st.success?) rescue NotImplementedError, ArgumentError end + + def test_seed + bug3104 = '[ruby-core:29292]' + rand_1 = Random.new(-1).rand + assert_not_equal(rand_1, Random.new((1 << 31) -1).rand, "#{bug3104} (2)") + assert_not_equal(rand_1, Random.new((1 << 63) -1).rand, "#{bug3104} (2)") + end end |