Skip to content

Commit 237fc68

Browse files
committed
merges r26936 from trunk into ruby_1_9_1 and little refactoring.
-- * random.c (rb_reset_random_seed): set seed in this. [ruby-core:28655] -- * random.c: refactoring. * random.c (rand_srand): a new function that wraps rand_init and (re)initialization of the random seed as a VALUE. * random.c (genrand_int32, genrand_real, rb_f_rand, rb_f_srand): using rand_srand. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@28511 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 58a4828 commit 237fc68

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

ChangeLog

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
Wed May 26 13:27:18 2010 Yuki Sonoda (Yugui) <yugui@yugui.jp>
2+
3+
* random.c: refactoring.
4+
5+
* random.c (rand_srand): a new function that wraps
6+
rand_init and (re)initialization of the random seed as
7+
a VALUE.
8+
9+
* random.c (genrand_int32, genrand_real, rb_f_rand,
10+
rb_f_srand): using rand_srand.
11+
12+
Mon Mar 15 11:49:48 2010 NARUSE, Yui <naruse@ruby-lang.org>
13+
14+
* random.c (rb_reset_random_seed): set seed in this.
15+
[ruby-core:28655]
16+
117
Mon Jun 29 20:29:11 2009 Tadayoshi Funaba <tadf@dotrb.org>
218

319
* rational.c (float_to_r): always returns rational.

random.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,28 @@ struct Random {
205205

206206
static struct Random default_mt;
207207

208+
static VALUE rand_init(struct MT *mt, VALUE vseed);
209+
static VALUE random_seed(void);
210+
static VALUE rand_srand(struct Random *rand, VALUE seed);
211+
208212
unsigned long
209213
rb_genrand_int32(void)
210214
{
211-
return genrand_int32(&default_mt.mt);
215+
struct MT *mt = &default_mt.mt;
216+
if (!genrand_initialized(mt)) {
217+
rand_srand(&default_mt, random_seed());
218+
}
219+
return genrand_int32(mt);
212220
}
213221

214222
double
215223
rb_genrand_real(void)
216224
{
217-
return genrand_real(&default_mt.mt);
225+
struct MT *mt = &default_mt.mt;
226+
if (!genrand_initialized(mt)) {
227+
rand_srand(&default_mt, random_seed());
228+
}
229+
return genrand_real(mt);
218230
}
219231

220232
static VALUE
@@ -337,6 +349,15 @@ random_seed(void)
337349
return make_seed_value(buf);
338350
}
339351

352+
static VALUE
353+
rand_srand(struct Random *rand, VALUE seed)
354+
{
355+
VALUE old = rand->seed.value;
356+
rand->seed.value = rand_init(&rand->mt, seed);
357+
358+
return old;
359+
}
360+
340361
/*
341362
* call-seq:
342363
* srand(number=0) => old_seed
@@ -354,7 +375,7 @@ random_seed(void)
354375
static VALUE
355376
rb_f_srand(int argc, VALUE *argv, VALUE obj)
356377
{
357-
VALUE seed, old;
378+
VALUE seed;
358379

359380
rb_secure(4);
360381
if (argc == 0) {
@@ -363,10 +384,7 @@ rb_f_srand(int argc, VALUE *argv, VALUE obj)
363384
else {
364385
rb_scan_args(argc, argv, "01", &seed);
365386
}
366-
old = default_mt.seed.value;
367-
default_mt.seed.value = rand_init(&default_mt.mt, seed);
368-
369-
return old;
387+
return rand_srand(&default_mt, seed);
370388
}
371389

372390
static unsigned long
@@ -481,7 +499,7 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj)
481499

482500
rb_scan_args(argc, argv, "01", &vmax);
483501
if (!genrand_initialized(mt)) {
484-
rand_init(mt, random_seed());
502+
rand_srand(&default_mt, random_seed());
485503
}
486504
switch (TYPE(vmax)) {
487505
case T_FLOAT:

test/ruby/test_rand.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,14 @@ def test_shuffle
164164
srand(0)
165165
assert_equal([1,4,2,5,3], [1,2,3,4,5].shuffle)
166166
end
167+
168+
def test_fork_shuffle
169+
pid = fork do
170+
(1..10).to_a.shuffle
171+
raise 'default seed is not set' if srand == 0
172+
end
173+
p2, st = Process.waitpid2(pid)
174+
assert(st.success?)
175+
rescue NotImplementedError, ArgumentError
176+
end
167177
end

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define RUBY_VERSION "1.9.1"
2-
#define RUBY_PATCHLEVEL 427
2+
#define RUBY_PATCHLEVEL 428
33
#define RUBY_VERSION_MAJOR 1
44
#define RUBY_VERSION_MINOR 9
55
#define RUBY_VERSION_TEENY 1

0 commit comments

Comments
 (0)