mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
reverted to r29091; r29092 breaks test-all
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29096 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
00dcf07ebc
commit
2531892cbc
4 changed files with 9 additions and 52 deletions
12
ChangeLog
12
ChangeLog
|
@ -1,15 +1,3 @@
|
|||
Wed Aug 25 18:31:17 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* random.c (rb_random_real): check the range of result.
|
||||
|
||||
* 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.
|
||||
|
||||
Wed Aug 25 03:42:43 2010 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||
|
||||
* ext/dl/cfunc.c (rb_dlcfunc_call): workaround for VC9 for x64.
|
||||
|
|
6
array.c
6
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 = rb_cRandom;
|
||||
VALUE *ptr, opts, randgen = Qnil;
|
||||
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 = rb_cRandom;
|
||||
VALUE opts, randgen = Qnil;
|
||||
long n, len, i, j, k, idx[10];
|
||||
|
||||
len = RARRAY_LEN(ary);
|
||||
|
@ -3826,7 +3826,6 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
|
|||
rb_scan_args(argc, argv, "1", &nv);
|
||||
n = NUM2LONG(nv);
|
||||
if (n < 0) rb_raise(rb_eArgError, "negative sample number");
|
||||
RB_GC_GUARD(ary) = ary_make_shared(ary);
|
||||
ptr = RARRAY_PTR(ary);
|
||||
len = RARRAY_LEN(ary);
|
||||
if (n > len) n = len;
|
||||
|
@ -3873,6 +3872,7 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
|
|||
VALUE *ptr_result;
|
||||
result = rb_ary_new4(len, ptr);
|
||||
ptr_result = RARRAY_PTR(result);
|
||||
RB_GC_GUARD(ary);
|
||||
for (i=0; i<n; i++) {
|
||||
j = RAND_UPTO(len-i) + i;
|
||||
nv = ptr_result[j];
|
||||
|
|
30
random.c
30
random.c
|
@ -229,20 +229,15 @@ static rb_random_t default_rand;
|
|||
static VALUE rand_init(struct MT *mt, VALUE vseed);
|
||||
static VALUE random_seed(void);
|
||||
|
||||
static rb_random_t *
|
||||
rand_start(rb_random_t *r)
|
||||
static struct MT *
|
||||
default_mt(void)
|
||||
{
|
||||
rb_random_t *r = &default_rand;
|
||||
struct MT *mt = &r->mt;
|
||||
if (!genrand_initialized(mt)) {
|
||||
r->seed = rand_init(mt, random_seed());
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static struct MT *
|
||||
default_mt(void)
|
||||
{
|
||||
return &rand_start(&default_rand)->mt;
|
||||
return mt;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
|
@ -368,9 +363,6 @@ 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);
|
||||
}
|
||||
|
@ -887,13 +879,7 @@ rb_random_int32(VALUE obj)
|
|||
{
|
||||
rb_random_t *rnd = try_get_rnd(obj);
|
||||
if (!rnd) {
|
||||
#if SIZEOF_LONG * CHAR_BIT > 32
|
||||
VALUE lim = ULONG2NUM(0x100000000);
|
||||
#elif defined HAVE_LONG_LONG
|
||||
VALUE lim = ULL2NUM((LONG_LONG)0xffffffff+1);
|
||||
#else
|
||||
VALUE lim = rb_big_plus(ULONG2NUM(0xffffffff), INT2FIX(1));
|
||||
#endif
|
||||
VALUE lim = ULONG2NUM(0xffffffff);
|
||||
return NUM2ULONG(rb_funcall2(obj, id_rand, 1, &lim));
|
||||
}
|
||||
return genrand_int32(&rnd->mt);
|
||||
|
@ -905,11 +891,7 @@ rb_random_real(VALUE obj)
|
|||
rb_random_t *rnd = try_get_rnd(obj);
|
||||
if (!rnd) {
|
||||
VALUE v = rb_funcall2(obj, id_rand, 0, 0);
|
||||
double d = NUM2DBL(v);
|
||||
if (d < 0.0 || d >= 1.0) {
|
||||
rb_raise(rb_eRangeError, "random number too big %g", d);
|
||||
}
|
||||
return d;
|
||||
return NUM2DBL(v);
|
||||
}
|
||||
return genrand_real(&rnd->mt);
|
||||
}
|
||||
|
|
|
@ -1899,19 +1899,6 @@ class TestArray < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_shuffle_random
|
||||
cc = nil
|
||||
gen = proc do
|
||||
10000000
|
||||
end
|
||||
class << gen
|
||||
alias rand call
|
||||
end
|
||||
assert_raise(RangeError) {
|
||||
[*0..2].shuffle(random: gen)
|
||||
}
|
||||
end
|
||||
|
||||
def test_sample
|
||||
100.times do
|
||||
assert([0, 1, 2].include?([2, 1, 0].sample))
|
||||
|
|
Loading…
Reference in a new issue