[前][次][番号順一覧][スレッド一覧]

ruby-changes:15365

From: nobu <ko1@a...>
Date: Thu, 8 Apr 2010 07:23:29 +0900 (JST)
Subject: [ruby-changes:15365] Ruby:r27255 (trunk): * random.c (rand_init): ignore higher bits if all they are same as

nobu	2010-04-08 07:22:36 +0900 (Thu, 08 Apr 2010)

  New Revision: 27255

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=27255

  Log:
    * random.c (rand_init): ignore higher bits if all they are same as
      the lower sign bit.  [ruby-core:29292](2)

  Modified files:
    trunk/ChangeLog
    trunk/random.c
    trunk/test/ruby/test_rand.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 27254)
+++ ChangeLog	(revision 27255)
@@ -1,3 +1,8 @@
+Thu Apr  8 07:22:05 2010  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@r...>
 
 	* lib/irb/cmd/help.rb (IRB::ExtendCommand::Help#execute): use RI
Index: test/ruby/test_rand.rb
===================================================================
--- test/ruby/test_rand.rb	(revision 27254)
+++ test/ruby/test_rand.rb	(revision 27255)
@@ -398,4 +398,11 @@
     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
Index: random.c
===================================================================
--- random.c	(revision 27254)
+++ random.c	(revision 27255)
@@ -367,6 +367,7 @@
 {
     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 @@
     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:

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]