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
This commit is contained in:
parent
023561f704
commit
d64c926eba
12
ChangeLog
12
ChangeLog
@ -1,3 +1,15 @@
|
|||||||
|
Sun Feb 24 15:33:46 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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@segment7.net>
|
Sun Feb 24 15:14:43 2013 Eric Hodel <drbrain@segment7.net>
|
||||||
|
|
||||||
* lib/net/http.rb: Removed duplicate Accept-Encoding in Net::HTTP#get.
|
* lib/net/http.rb: Removed duplicate Accept-Encoding in Net::HTTP#get.
|
||||||
|
15
random.c
15
random.c
@ -942,13 +942,26 @@ rb_random_real(VALUE obj)
|
|||||||
return genrand_real(&rnd->mt);
|
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
|
unsigned long
|
||||||
rb_random_ulong_limited(VALUE obj, unsigned long limit)
|
rb_random_ulong_limited(VALUE obj, unsigned long limit)
|
||||||
{
|
{
|
||||||
rb_random_t *rnd = try_get_rnd(obj);
|
rb_random_t *rnd = try_get_rnd(obj);
|
||||||
if (!rnd) {
|
if (!rnd) {
|
||||||
extern int rb_num_negative_p(VALUE);
|
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);
|
VALUE v = rb_funcall2(obj, id_rand, 1, &lim);
|
||||||
unsigned long r = NUM2ULONG(v);
|
unsigned long r = NUM2ULONG(v);
|
||||||
if (rb_num_negative_p(v)) {
|
if (rb_num_negative_p(v)) {
|
||||||
|
@ -528,5 +528,13 @@ END
|
|||||||
def (gen = Object.new).rand(*) -1 end
|
def (gen = Object.new).rand(*) -1 end
|
||||||
e = assert_raise(RangeError) {[1,2,3].sample(random: gen)}
|
e = assert_raise(RangeError) {[1,2,3].sample(random: gen)}
|
||||||
assert_match(/small -1\z/, e.message, bug7903)
|
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
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user