* 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:
nobu 2010-08-25 09:00:17 +00:00
parent 569d821929
commit ae6a9009dc
3 changed files with 20 additions and 7 deletions

View File

@ -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 * array.c (rb_ary_sample): use frozen shared array to get rid of
reallocation. reallocation.

View File

@ -3748,7 +3748,7 @@ static VALUE sym_random;
static VALUE static VALUE
rb_ary_shuffle_bang(int argc, VALUE *argv, VALUE ary) 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); long i = RARRAY_LEN(ary);
if (OPTHASH_GIVEN_P(opts)) { if (OPTHASH_GIVEN_P(opts)) {
@ -3811,7 +3811,7 @@ static VALUE
rb_ary_sample(int argc, VALUE *argv, VALUE ary) rb_ary_sample(int argc, VALUE *argv, VALUE ary)
{ {
VALUE nv, result, *ptr; VALUE nv, result, *ptr;
VALUE opts, randgen = Qnil; VALUE opts, randgen = rb_cRandom;
long n, len, i, j, k, idx[10]; long n, len, i, j, k, idx[10];
len = RARRAY_LEN(ary); len = RARRAY_LEN(ary);

View File

@ -229,15 +229,20 @@ static rb_random_t default_rand;
static VALUE rand_init(struct MT *mt, VALUE vseed); static VALUE rand_init(struct MT *mt, VALUE vseed);
static VALUE random_seed(void); static VALUE random_seed(void);
static struct MT * static rb_random_t *
default_mt(void) rand_start(rb_random_t *r)
{ {
rb_random_t *r = &default_rand;
struct MT *mt = &r->mt; struct MT *mt = &r->mt;
if (!genrand_initialized(mt)) { if (!genrand_initialized(mt)) {
r->seed = rand_init(mt, random_seed()); 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 unsigned int
@ -363,6 +368,9 @@ get_rnd(VALUE obj)
static rb_random_t * static rb_random_t *
try_get_rnd(VALUE obj) 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; if (!rb_typeddata_is_kind_of(obj, &random_data_type)) return NULL;
return DATA_PTR(obj); return DATA_PTR(obj);
} }