diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-04-07 22:22:36 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-04-07 22:22:36 +0000 |
commit | f571492922083d0a02d2dc5ee5125af52935d5cd (patch) | |
tree | 57356bc5b6b0883e8b98c6331b0a7b4e409061d0 | |
parent | 0f58f9dbe7bf81595b78bcee9cab49a4a546ba80 (diff) | |
download | ruby-f571492922083d0a02d2dc5ee5125af52935d5cd.tar.gz |
* random.c (rand_init): ignore higher bits if all they are same as
the lower sign bit. [ruby-core:29292](2)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27255 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-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 |