ruby-changes:15060
From: naruse <ko1@a...>
Date: Mon, 15 Mar 2010 12:27:14 +0900 (JST)
Subject: [ruby-changes:15060] Ruby:r26936 (trunk): * random.c (rb_reset_random_seed): set seed in this.
naruse 2010-03-15 12:26:29 +0900 (Mon, 15 Mar 2010) New Revision: 26936 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26936 Log: * random.c (rb_reset_random_seed): set seed in this. [ruby-core:28655] Modified files: trunk/ChangeLog trunk/random.c trunk/test/ruby/test_rand.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 26935) +++ ChangeLog (revision 26936) @@ -1,3 +1,8 @@ +Mon Mar 15 11:49:48 2010 NARUSE, Yui <naruse@r...> + + * random.c (rb_reset_random_seed): set seed in this. + [ruby-core:28655] + Mon Mar 15 10:26:02 2010 NARUSE, Yui <naruse@r...> * ext/nkf/nkf-utf8/nkf.c: import latest nkf. [master 9306cb0] Index: test/ruby/test_rand.rb =================================================================== --- test/ruby/test_rand.rb (revision 26935) +++ test/ruby/test_rand.rb (revision 26936) @@ -387,4 +387,14 @@ r2.rand(0x100) assert(r1 == r2) end + + def test_fork_shuffle + pid = fork do + (1..10).to_a.shuffle + raise 'default seed is not set' if srand == 0 + end + p2, st = Process.waitpid2(pid) + assert(st.success?) + rescue NotImplementedError, ArgumentError + end end Index: random.c =================================================================== --- random.c (revision 26935) +++ random.c (revision 26936) @@ -223,16 +223,34 @@ static struct Random default_rand; +static VALUE rand_init(struct MT *mt, VALUE vseed); +static VALUE random_seed(void); + +static void +default_rand_init(void) +{ + rb_random_t *r = &default_rand.rnd; + r->seed = rand_init(&r->mt, random_seed()); +} + unsigned int rb_genrand_int32(void) { - return genrand_int32(&default_rand.rnd.mt); + struct MT *mt = &default_rand.rnd.mt; + if (!genrand_initialized(mt)) { + default_rand_init(); + } + return genrand_int32(mt); } double rb_genrand_real(void) { - return genrand_real(&default_rand.rnd.mt); + struct MT *mt = &default_rand.rnd.mt; + if (!genrand_initialized(mt)) { + default_rand_init(); + } + return genrand_real(mt); } #define BDIGITS(x) (RBIGNUM_DIGITS(x)) @@ -303,8 +321,6 @@ #define id_minus '-' #define id_plus '+' -static VALUE random_seed(void); - /* :nodoc: */ static void random_mark(void *ptr) @@ -831,7 +847,7 @@ { struct MT *mt = &default_rand.rnd.mt; if (!genrand_initialized(mt)) { - rand_init(mt, random_seed()); + default_rand_init(); } return limited_rand(mt, i); } @@ -1111,7 +1127,7 @@ struct MT *mt = &default_rand.rnd.mt; if (!genrand_initialized(mt)) { - rand_init(mt, random_seed()); + default_rand_init(); } if (argc == 0) goto zero_arg; rb_scan_args(argc, argv, "01", &vmax); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/