From 0c2b3f7f4f1628b340d2e08614db608f0e54f96e Mon Sep 17 00:00:00 2001 From: nagachika Date: Sat, 9 Mar 2013 14:57:48 +0000 Subject: 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 --- random.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'random.c') diff --git a/random.c b/random.c index 9b62cc1add..2ef8959c26 100644 --- a/random.c +++ b/random.c @@ -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)) { -- cgit v1.2.3