mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
array.c: use rb_random_ulong_limited
* array.c (rb_ary_sample): use rb_random_ulong_limited, since precision of long may be larger than double. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7f8e558464
commit
e3efce6df1
5 changed files with 41 additions and 22 deletions
|
@ -1,4 +1,7 @@
|
||||||
Tue Oct 9 17:59:00 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Tue Oct 9 18:01:37 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* array.c (rb_ary_sample): use rb_random_ulong_limited, since
|
||||||
|
precision of long may be larger than double.
|
||||||
|
|
||||||
* random.c (rb_random_ulong_limited): new function to return a random
|
* random.c (rb_random_ulong_limited): new function to return a random
|
||||||
value from 0 upto limit as unsigned long, simillary to
|
value from 0 upto limit as unsigned long, simillary to
|
||||||
|
|
5
NEWS
5
NEWS
|
@ -17,6 +17,11 @@ with all sufficient information, see the ChangeLog file.
|
||||||
|
|
||||||
* builtin classes
|
* builtin classes
|
||||||
|
|
||||||
|
* Array
|
||||||
|
* incompatible changes:
|
||||||
|
* random parameter of Array#shuffle! and Array#sample now
|
||||||
|
will be called with one argument, maximum value.
|
||||||
|
|
||||||
* Enumerable
|
* Enumerable
|
||||||
* added method:
|
* added method:
|
||||||
* added Enumerable#lazy method for lazy enumeration.
|
* added Enumerable#lazy method for lazy enumeration.
|
||||||
|
|
40
array.c
40
array.c
|
@ -3996,7 +3996,7 @@ rb_ary_flatten(int argc, VALUE *argv, VALUE ary)
|
||||||
(argc > 0 && !NIL_P((opts) = rb_check_hash_type(argv[argc-1])) && (--argc, 1))
|
(argc > 0 && !NIL_P((opts) = rb_check_hash_type(argv[argc-1])) && (--argc, 1))
|
||||||
static VALUE sym_random;
|
static VALUE sym_random;
|
||||||
|
|
||||||
#define RAND_UPTO(max) (long)(rb_random_real(randgen)*(max))
|
#define RAND_UPTO(max) (long)rb_random_ulong_limited((randgen), (max)-1)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
|
@ -4091,7 +4091,7 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
|
||||||
VALUE nv, result, *ptr;
|
VALUE nv, result, *ptr;
|
||||||
VALUE opts, randgen = rb_cRandom;
|
VALUE opts, randgen = rb_cRandom;
|
||||||
long n, len, i, j, k, idx[10];
|
long n, len, i, j, k, idx[10];
|
||||||
double rnds[numberof(idx)];
|
long rnds[numberof(idx)];
|
||||||
|
|
||||||
if (OPTHASH_GIVEN_P(opts)) {
|
if (OPTHASH_GIVEN_P(opts)) {
|
||||||
randgen = rb_hash_lookup2(opts, sym_random, randgen);
|
randgen = rb_hash_lookup2(opts, sym_random, randgen);
|
||||||
|
@ -4104,11 +4104,11 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
double x = rb_random_real(randgen);
|
i = RAND_UPTO(len);
|
||||||
if ((len = RARRAY_LEN(ary)) == 0) return Qnil;
|
if ((len = RARRAY_LEN(ary)) <= i) return Qnil;
|
||||||
i = (long)(x * len);
|
ptr = RARRAY_PTR(ary);
|
||||||
}
|
}
|
||||||
return RARRAY_PTR(ary)[i];
|
return ptr[i];
|
||||||
}
|
}
|
||||||
rb_scan_args(argc, argv, "1", &nv);
|
rb_scan_args(argc, argv, "1", &nv);
|
||||||
n = NUM2LONG(nv);
|
n = NUM2LONG(nv);
|
||||||
|
@ -4116,27 +4116,37 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
|
||||||
if (n > len) n = len;
|
if (n > len) n = len;
|
||||||
if (n <= numberof(idx)) {
|
if (n <= numberof(idx)) {
|
||||||
for (i = 0; i < n; ++i) {
|
for (i = 0; i < n; ++i) {
|
||||||
rnds[i] = rb_random_real(randgen);
|
rnds[i] = RAND_UPTO(len - i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
k = len;
|
||||||
len = RARRAY_LEN(ary);
|
len = RARRAY_LEN(ary);
|
||||||
ptr = RARRAY_PTR(ary);
|
ptr = RARRAY_PTR(ary);
|
||||||
|
if (len < k) {
|
||||||
|
if (n <= numberof(idx)) {
|
||||||
|
for (i = 0; i < n; ++i) {
|
||||||
|
if (rnds[i] >= len) {
|
||||||
|
return rb_ary_new2(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (n > len) n = len;
|
if (n > len) n = len;
|
||||||
switch (n) {
|
switch (n) {
|
||||||
case 0:
|
case 0:
|
||||||
return rb_ary_new2(0);
|
return rb_ary_new2(0);
|
||||||
case 1:
|
case 1:
|
||||||
i = (long)(rnds[0] * len);
|
i = rnds[0];
|
||||||
return rb_ary_new4(1, &ptr[i]);
|
return rb_ary_new4(1, &ptr[i]);
|
||||||
case 2:
|
case 2:
|
||||||
i = (long)(rnds[0] * len);
|
i = rnds[0];
|
||||||
j = (long)(rnds[1] * (len-1));
|
j = rnds[1];
|
||||||
if (j >= i) j++;
|
if (j >= i) j++;
|
||||||
return rb_ary_new3(2, ptr[i], ptr[j]);
|
return rb_ary_new3(2, ptr[i], ptr[j]);
|
||||||
case 3:
|
case 3:
|
||||||
i = (long)(rnds[0] * len);
|
i = rnds[0];
|
||||||
j = (long)(rnds[1] * (len-1));
|
j = rnds[1];
|
||||||
k = (long)(rnds[2] * (len-2));
|
k = rnds[2];
|
||||||
{
|
{
|
||||||
long l = j, g = i;
|
long l = j, g = i;
|
||||||
if (j >= i) l = i, g = ++j;
|
if (j >= i) l = i, g = ++j;
|
||||||
|
@ -4147,9 +4157,9 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
|
||||||
if (n <= numberof(idx)) {
|
if (n <= numberof(idx)) {
|
||||||
VALUE *ptr_result;
|
VALUE *ptr_result;
|
||||||
long sorted[numberof(idx)];
|
long sorted[numberof(idx)];
|
||||||
sorted[0] = idx[0] = (long)(rnds[0] * len);
|
sorted[0] = idx[0] = rnds[0];
|
||||||
for (i=1; i<n; i++) {
|
for (i=1; i<n; i++) {
|
||||||
k = (long)(rnds[i] * --len);
|
k = rnds[i];
|
||||||
for (j = 0; j < i; ++j) {
|
for (j = 0; j < i; ++j) {
|
||||||
if (k < sorted[j]) break;
|
if (k < sorted[j]) break;
|
||||||
++k;
|
++k;
|
||||||
|
|
|
@ -2019,15 +2019,15 @@ class TestArray < Test::Unit::TestCase
|
||||||
def test_sample_random
|
def test_sample_random
|
||||||
ary = (0...10000).to_a
|
ary = (0...10000).to_a
|
||||||
assert_raise(ArgumentError) {ary.sample(1, 2, random: nil)}
|
assert_raise(ArgumentError) {ary.sample(1, 2, random: nil)}
|
||||||
gen0 = proc do
|
gen0 = proc do |max|
|
||||||
0.5
|
(max+1)/2
|
||||||
end
|
end
|
||||||
class << gen0
|
class << gen0
|
||||||
alias rand call
|
alias rand call
|
||||||
end
|
end
|
||||||
gen1 = proc do
|
gen1 = proc do |max|
|
||||||
ary.replace([])
|
ary.replace([])
|
||||||
0.5
|
(max+1)/2
|
||||||
end
|
end
|
||||||
class << gen1
|
class << gen1
|
||||||
alias rand call
|
alias rand call
|
||||||
|
|
|
@ -170,8 +170,9 @@ class TestRand < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_shuffle
|
def test_shuffle
|
||||||
srand(0)
|
srand(0)
|
||||||
assert_equal([1,4,2,5,3], [1,2,3,4,5].shuffle)
|
result = [*1..5].shuffle
|
||||||
assert_equal([1,4,2,5,3], [1,2,3,4,5].shuffle(random: Random.new(0)))
|
assert_equal([*1..5], result.sort)
|
||||||
|
assert_equal(result, [*1..5].shuffle(random: Random.new(0)))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_big_seed
|
def test_big_seed
|
||||||
|
|
Loading…
Add table
Reference in a new issue