зеркало из https://github.com/github/ruby.git
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
This commit is contained in:
Родитель
dd83dd6bda
Коммит
7f8e558464
|
@ -1,3 +1,9 @@
|
|||
Tue Oct 9 17:59:00 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* 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.
|
||||
|
||||
Tue Oct 9 17:13:27 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* process.c (rb_execarg_addopt, rb_execarg_run_options): add :uid and
|
||||
|
|
|
@ -610,6 +610,7 @@ VALUE rb_random_bytes(VALUE rnd, long n);
|
|||
VALUE rb_random_int(VALUE rnd, VALUE max);
|
||||
unsigned int rb_random_int32(VALUE rnd);
|
||||
double rb_random_real(VALUE rnd);
|
||||
unsigned long rb_random_ulong_limited(VALUE rnd, unsigned long limit);
|
||||
unsigned long rb_genrand_ulong_limited(unsigned long i);
|
||||
/* re.c */
|
||||
#define rb_memcmp memcmp
|
||||
|
|
16
random.c
16
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
|
||||
*
|
||||
|
|
Загрузка…
Ссылка в новой задаче