diff --git a/ChangeLog b/ChangeLog index 67b7dda90e..f2b4e1c629 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Mar 15 11:49:48 2010 NARUSE, Yui + + * random.c (rb_reset_random_seed): set seed in this. + [ruby-core:28655] + Mon Mar 15 10:26:02 2010 NARUSE, Yui * ext/nkf/nkf-utf8/nkf.c: import latest nkf. [master 9306cb0] diff --git a/random.c b/random.c index bf78e5049e..d36853f6b3 100644 --- a/random.c +++ b/random.c @@ -223,16 +223,34 @@ struct Random { 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 @@ VALUE rb_cRandom; #define id_minus '-' #define id_plus '+' -static VALUE random_seed(void); - /* :nodoc: */ static void random_mark(void *ptr) @@ -831,7 +847,7 @@ rb_rand_internal(unsigned long i) { 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 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj) 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); diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb index b6563954cf..97ac8147bd 100644 --- a/test/ruby/test_rand.rb +++ b/test/ruby/test_rand.rb @@ -387,4 +387,14 @@ END 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