ruby-changes:27414
From: nobu <ko1@a...>
Date: Sun, 24 Feb 2013 15:35:12 +0900 (JST)
Subject: [ruby-changes:27414] nobu:r39466 (trunk): random.c: increase limit for generic rand
nobu 2013-02-24 15:33:49 +0900 (Sun, 24 Feb 2013) New Revision: 39466 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39466 Log: 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] Modified files: trunk/ChangeLog trunk/random.c trunk/test/ruby/test_rand.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 39465) +++ ChangeLog (revision 39466) @@ -1,3 +1,15 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun Feb 24 15:33:46 2013 Nobuyoshi Nakada <nobu@r...> + + * 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] + +Sun Feb 24 15:32:36 2013 Nobuyoshi Nakada <nobu@r...> + + * 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] + Sun Feb 24 15:14:43 2013 Eric Hodel <drbrain@s...> * lib/net/http.rb: Removed duplicate Accept-Encoding in Net::HTTP#get. Index: test/ruby/test_rand.rb =================================================================== --- test/ruby/test_rand.rb (revision 39465) +++ test/ruby/test_rand.rb (revision 39466) @@ -528,5 +528,13 @@ END https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L528 def (gen = Object.new).rand(*) -1 end e = assert_raise(RangeError) {[1,2,3].sample(random: gen)} assert_match(/small -1\z/, e.message, bug7903) + + bug7935 = '[ruby-core:52779] [Bug #7935]' + class << (gen = Object.new) + def rand(limit) @limit = limit; 0 end + attr_reader :limit + end + [1, 2].sample(1, random: gen) + assert_equal(2, gen.limit, bug7935) end end Index: random.c =================================================================== --- random.c (revision 39465) +++ random.c (revision 39466) @@ -942,13 +942,26 @@ rb_random_real(VALUE obj) https://github.com/ruby/ruby/blob/trunk/random.c#L942 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)) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/