mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* random.c (random_rand): fixed for edge cases of ranges.
[ruby-dev:39166] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24674 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0170c2fc4d
commit
31875e97b4
3 changed files with 20 additions and 3 deletions
|
@ -1,3 +1,8 @@
|
|||
Wed Aug 26 23:58:39 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* random.c (random_rand): fixed for edge cases of ranges.
|
||||
[ruby-dev:39166]
|
||||
|
||||
Wed Aug 26 21:49:23 2009 NARUSE, Yui <naruse@ruby-lang.org>
|
||||
|
||||
* lib/tempfile.rb: add documents from Hongli Lai's fork.
|
||||
|
|
10
random.c
10
random.c
|
@ -760,10 +760,11 @@ make_mask(unsigned long x)
|
|||
static unsigned long
|
||||
limited_rand(struct MT *mt, unsigned long limit)
|
||||
{
|
||||
unsigned long mask = make_mask(limit);
|
||||
int i;
|
||||
unsigned long val;
|
||||
unsigned long val, mask;
|
||||
|
||||
if (!limit) return 0;
|
||||
mask = make_mask(limit);
|
||||
retry:
|
||||
val = 0;
|
||||
for (i = SIZEOF_LONG/SIZEOF_INT32-1; 0 <= i; i--) {
|
||||
|
@ -1016,7 +1017,7 @@ random_rand(int argc, VALUE *argv, VALUE obj)
|
|||
v = Qnil;
|
||||
if (FIXNUM_P(vmax)) {
|
||||
fixnum:
|
||||
if ((max = FIX2LONG(vmax) - excl) > 0) {
|
||||
if ((max = FIX2LONG(vmax) - excl) >= 0) {
|
||||
unsigned long r = limited_rand(&rnd->mt, (unsigned long)max);
|
||||
v = ULONG2NUM(r);
|
||||
}
|
||||
|
@ -1042,6 +1043,9 @@ random_rand(int argc, VALUE *argv, VALUE obj)
|
|||
}
|
||||
v = rb_float_new(r * max);
|
||||
}
|
||||
else if (max == 0.0 && !excl) {
|
||||
v = rb_float_new(0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -199,6 +199,14 @@ class TestRand < Test::Unit::TestCase
|
|||
assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(-1) }
|
||||
assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(-1.0) }
|
||||
assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(0) }
|
||||
assert_equal(0, r.rand(1), '[ruby-dev:39166]')
|
||||
assert_equal(0, r.rand(0...1), '[ruby-dev:39166]')
|
||||
assert_equal(0, r.rand(0..0), '[ruby-dev:39166]')
|
||||
assert_equal(0.0, r.rand(0.0..0.0), '[ruby-dev:39166]')
|
||||
assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0...0) }
|
||||
assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0..-1) }
|
||||
assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0.0...0.0) }
|
||||
assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0.0...-0.1) }
|
||||
end
|
||||
|
||||
def test_random_seed
|
||||
|
|
Loading…
Add table
Reference in a new issue