* array.c (rb_ary_{shuffle_bang,sample}): use Random class object.
* random.c (try_get_rnd): use default_rand for Random as same as singleton methods. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29093 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
569d821929
commit
ae6a9009dc
@ -1,4 +1,9 @@
|
||||
Wed Aug 25 17:56:40 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
Wed Aug 25 17:59:50 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* array.c (rb_ary_{shuffle_bang,sample}): use Random class object.
|
||||
|
||||
* random.c (try_get_rnd): use default_rand for Random as same as
|
||||
singleton methods.
|
||||
|
||||
* array.c (rb_ary_sample): use frozen shared array to get rid of
|
||||
reallocation.
|
||||
|
4
array.c
4
array.c
@ -3748,7 +3748,7 @@ static VALUE sym_random;
|
||||
static VALUE
|
||||
rb_ary_shuffle_bang(int argc, VALUE *argv, VALUE ary)
|
||||
{
|
||||
VALUE *ptr, opts, randgen = Qnil;
|
||||
VALUE *ptr, opts, randgen = rb_cRandom;
|
||||
long i = RARRAY_LEN(ary);
|
||||
|
||||
if (OPTHASH_GIVEN_P(opts)) {
|
||||
@ -3811,7 +3811,7 @@ static VALUE
|
||||
rb_ary_sample(int argc, VALUE *argv, VALUE ary)
|
||||
{
|
||||
VALUE nv, result, *ptr;
|
||||
VALUE opts, randgen = Qnil;
|
||||
VALUE opts, randgen = rb_cRandom;
|
||||
long n, len, i, j, k, idx[10];
|
||||
|
||||
len = RARRAY_LEN(ary);
|
||||
|
16
random.c
16
random.c
@ -229,15 +229,20 @@ static rb_random_t default_rand;
|
||||
static VALUE rand_init(struct MT *mt, VALUE vseed);
|
||||
static VALUE random_seed(void);
|
||||
|
||||
static struct MT *
|
||||
default_mt(void)
|
||||
static rb_random_t *
|
||||
rand_start(rb_random_t *r)
|
||||
{
|
||||
rb_random_t *r = &default_rand;
|
||||
struct MT *mt = &r->mt;
|
||||
if (!genrand_initialized(mt)) {
|
||||
r->seed = rand_init(mt, random_seed());
|
||||
}
|
||||
return mt;
|
||||
return r;
|
||||
}
|
||||
|
||||
static struct MT *
|
||||
default_mt(void)
|
||||
{
|
||||
return &rand_start(&default_rand)->mt;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
@ -363,6 +368,9 @@ get_rnd(VALUE obj)
|
||||
static rb_random_t *
|
||||
try_get_rnd(VALUE obj)
|
||||
{
|
||||
if (obj == rb_cRandom) {
|
||||
return rand_start(&default_rand);
|
||||
}
|
||||
if (!rb_typeddata_is_kind_of(obj, &random_data_type)) return NULL;
|
||||
return DATA_PTR(obj);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user