summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-10-09 08:59:03 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-10-09 08:59:03 +0000
commit7f8e5584649b1bba6d30d78c661a1b1eca28340e (patch)
tree33905f3ad17c9ea5fba4476602b6e61a7b5e5f95 /random.c
parentdd83dd6bda20f701ce637031532be14a165f1de1 (diff)
random.c: rb_random_ulong_limited
* random.c (rb_random_ulong_limited): new function to return a random value from 0 upto limit as unsigned long, simillary to rb_genrand_ulong_limited but with arbitrary RNG object. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37125 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r--random.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/random.c b/random.c
index b36784c405..ccae2a0178 100644
--- a/random.c
+++ b/random.c
@@ -943,6 +943,22 @@ rb_random_real(VALUE obj)
return genrand_real(&rnd->mt);
}
+unsigned long
+rb_random_ulong_limited(VALUE obj, unsigned long limit)
+{
+ rb_random_t *rnd = try_get_rnd(obj);
+ if (!rnd) {
+ VALUE lim = ULONG2NUM(limit);
+ VALUE v = rb_funcall2(obj, id_rand, 1, &lim);
+ unsigned long r = NUM2ULONG(v);
+ if (r > limit) {
+ rb_raise(rb_eRangeError, "random number too big %ld", r);
+ }
+ return r;
+ }
+ return limited_rand(&rnd->mt, limit);
+}
+
/*
* call-seq: prng.bytes(size) -> a_string
*