зеркало из https://github.com/github/ruby.git
* 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
This commit is contained in:
Родитель
e03b66ef18
Коммит
1bf3ca494f
|
@ -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]
|
||||
|
|
28
random.c
28
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);
|
||||
|
|
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче