summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-15 03:26:29 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-15 03:26:29 +0000
commit1bf3ca494f89e56ca267b30ce85ff100994e7b64 (patch)
treeed044251e1744c6b1ded906759082321cb30d6a0
parente03b66ef183ec828dc9422d514af750915767e77 (diff)
* random.c (rb_reset_random_seed): set seed in this. [ruby-core:28655]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26936 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--random.c28
-rw-r--r--test/ruby/test_rand.rb10
3 files changed, 37 insertions, 6 deletions
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 <naruse@ruby-lang.org>
+
+ * random.c (rb_reset_random_seed): set seed in this.
+ [ruby-core:28655]
+
Mon Mar 15 10:26:02 2010 NARUSE, Yui <naruse@ruby-lang.org>
* 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