diff options
author | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-03-09 14:57:48 +0000 |
---|---|---|
committer | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-03-09 14:57:48 +0000 |
commit | 0c2b3f7f4f1628b340d2e08614db608f0e54f96e (patch) | |
tree | 22ef91343761b4dbac662eaec94913eb0d8cb669 /random.c | |
parent | 8767705eb238d7c2391c5cce3c2945db99fc0c24 (diff) |
merge revision(s) 39466,39470: [Backport #7935]
* random.c (rb_random_ulong_limited): limit is inclusive, but generic
rand method should return a number less than it, so increase for the
difference. [ruby-core:52779] [Bug #7935]
* test/ruby/test_array.rb (test_sample_random): remove adjustment for
the bug fixed by r39466. [ruby-core:52779] [Bug #7935]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@39675 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r-- | random.c | 15 |
1 files changed, 14 insertions, 1 deletions
@@ -942,13 +942,26 @@ rb_random_real(VALUE obj) return genrand_real(&rnd->mt); } +static inline VALUE +ulong_to_num_plus_1(unsigned long n) +{ +#if HAVE_LONG_LONG + return ULL2NUM((LONG_LONG)n+1); +#else + if (n >= ULONG_MAX) { + return rb_big_plus(ULONG2NUM(n), INT2FIX(1)); + } + return ULONG2NUM(n+1); +#endif +} + unsigned long rb_random_ulong_limited(VALUE obj, unsigned long limit) { rb_random_t *rnd = try_get_rnd(obj); if (!rnd) { extern int rb_num_negative_p(VALUE); - VALUE lim = ULONG2NUM(limit); + VALUE lim = ulong_to_num_plus_1(limit); VALUE v = rb_funcall2(obj, id_rand, 1, &lim); unsigned long r = NUM2ULONG(v); if (rb_num_negative_p(v)) { |