From 493f72185499f3fea1212b04d177a0870d6e89c3 Mon Sep 17 00:00:00 2001 From: naruse Date: Fri, 10 Sep 2010 05:44:54 +0000 Subject: [PATCH] * random.c (rb_genrand_ulong_limited): renamed from rb_rand_internal and now this is public API. * include/ruby/ruby.h (rb_genrand_ulong_limited): added. * bignum.c (big_sparse_p): use rb_genrand_ulong_limited. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++++++ bignum.c | 7 +++---- include/ruby/intern.h | 1 + random.c | 11 ++++++++--- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 074ecef985..01c46b56c2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Fri Sep 10 10:33:18 2010 NARUSE, Yui + + * random.c (rb_genrand_ulong_limited): renamed from + rb_rand_internal and now this is public API. + + * include/ruby/ruby.h (rb_genrand_ulong_limited): added. + + * bignum.c (big_sparse_p): use rb_genrand_ulong_limited. + Fri Sep 10 13:07:22 2010 NAKAMURA, Hiroshi * ext/digest/lib/digest.rb: removed unused exception variable diff --git a/bignum.c b/bignum.c index 1dd43df494..3145f24843 100644 --- a/bignum.c +++ b/bignum.c @@ -2214,11 +2214,10 @@ static inline VALUE big_sparse_p(VALUE x) { long c = 0, n = RBIGNUM_LEN(x); - unsigned long rb_rand_internal(unsigned long i); - if ( BDIGITS(x)[rb_rand_internal(n / 2) + n / 4]) c++; - if (c <= 1 && BDIGITS(x)[rb_rand_internal(n / 2) + n / 4]) c++; - if (c <= 1 && BDIGITS(x)[rb_rand_internal(n / 2) + n / 4]) c++; + if ( BDIGITS(x)[rb_genrand_ulong_limited(n / 2) + n / 4]) c++; + if (c <= 1 && BDIGITS(x)[rb_genrand_ulong_limited(n / 2) + n / 4]) c++; + if (c <= 1 && BDIGITS(x)[rb_genrand_ulong_limited(n / 2) + n / 4]) c++; return (c <= 1) ? Qtrue : Qfalse; } diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 0ae4d47157..2c79d700de 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -559,6 +559,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_genrand_ulong_limited(unsigned long i); /* re.c */ #define rb_memcmp memcmp int rb_memcicmp(const void*,const void*,long); diff --git a/random.c b/random.c index 64b1cec4b1..9044c77432 100644 --- a/random.c +++ b/random.c @@ -875,11 +875,16 @@ limited_big_rand(struct MT *mt, struct RBignum *limit) return rb_big_norm((VALUE)val); } +/* + * Returns random unsigned long value in [0, _limit_]. + * + * Note that _limit_ is included, and the range of the argument and the + * return value depends on environments. + */ unsigned long -rb_rand_internal(unsigned long i) +rb_genrand_ulong_limited(unsigned long limit) { - struct MT *mt = default_mt(); - return limited_rand(mt, i); + return limited_rand(default_mt(), limit); } unsigned int