diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-02-24 06:33:49 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-02-24 06:33:49 +0000 |
commit | d64c926eba3a317f4756e048d2ef7522c463636d (patch) | |
tree | eb1a3f91baa522fb5401b0f1a8c121d6a45e279f /random.c | |
parent | 023561f704f51804d0803ff2c1839901d609a83d (diff) | |
download | ruby-d64c926eba3a317f4756e048d2ef7522c463636d.tar.gz |
random.c: increase limit for generic rand
* 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]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39466 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)) { |