1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* random.c (rb_random_{int32,real,bytes,int}): added functions for

extension libraries.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2009-07-18 00:16:25 +00:00
parent 8195282dfc
commit f3d022543e
3 changed files with 35 additions and 5 deletions

View file

@ -1,3 +1,8 @@
Sat Jul 18 09:16:18 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* random.c (rb_random_{int32,real,bytes,int}): added functions for
extension libraries.
Sat Jul 18 09:07:00 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> Sat Jul 18 09:07:00 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* random.c (fill_random_seed): use cryptographic service on Windows. * random.c (fill_random_seed): use cryptographic service on Windows.

View file

@ -524,9 +524,13 @@ VALUE rb_range_new(VALUE, VALUE, int);
VALUE rb_range_beg_len(VALUE, long*, long*, long, int); VALUE rb_range_beg_len(VALUE, long*, long*, long, int);
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp); int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp);
/* random.c */ /* random.c */
unsigned long rb_genrand_int32(void); unsigned int rb_genrand_int32(void);
double rb_genrand_real(void); double rb_genrand_real(void);
void rb_reset_random_seed(void); void rb_reset_random_seed(void);
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);
/* re.c */ /* re.c */
#define rb_memcmp memcmp #define rb_memcmp memcmp
int rb_memcicmp(const void*,const void*,long); int rb_memcicmp(const void*,const void*,long);

View file

@ -211,7 +211,7 @@ struct Random {
static struct Random default_rand; static struct Random default_rand;
unsigned long unsigned int
rb_genrand_int32(void) rb_genrand_int32(void)
{ {
return genrand_int32(&default_rand.rnd.mt); return genrand_int32(&default_rand.rnd.mt);
@ -767,6 +767,20 @@ rb_rand_internal(unsigned long i)
return limited_rand(mt, i); return limited_rand(mt, i);
} }
unsigned int
rb_random_int32(VALUE obj)
{
rb_random_t *rnd = get_rnd(obj);
return genrand_int32(&rnd->mt);
}
double
rb_random_real(VALUE obj)
{
rb_random_t *rnd = get_rnd(obj);
return genrand_real(&rnd->mt);
}
/* /*
* call-seq: prng.bytes(size) -> prng * call-seq: prng.bytes(size) -> prng
* *
@ -775,9 +789,14 @@ rb_rand_internal(unsigned long i)
*/ */
static VALUE static VALUE
random_bytes(VALUE obj, VALUE len) random_bytes(VALUE obj, VALUE len)
{
return rb_random_bytes(obj, FIX2LONG(rb_to_int(len)));
}
VALUE
rb_random_bytes(VALUE obj, long n)
{ {
rb_random_t *rnd = get_rnd(obj); rb_random_t *rnd = get_rnd(obj);
long n = FIX2LONG(rb_to_int(len));
VALUE bytes = rb_str_new(0, n); VALUE bytes = rb_str_new(0, n);
char *ptr = RSTRING_PTR(bytes); char *ptr = RSTRING_PTR(bytes);
unsigned int r, i; unsigned int r, i;
@ -868,8 +887,8 @@ rand_int(struct MT *mt, VALUE vmax)
* *
* Otherwise, it raises an ArgumentError. * Otherwise, it raises an ArgumentError.
*/ */
static VALUE VALUE
random_int(VALUE obj, VALUE vmax) rb_random_int(VALUE obj, VALUE vmax)
{ {
VALUE v, beg = Qundef; VALUE v, beg = Qundef;
rb_random_t *rnd = get_rnd(obj); rb_random_t *rnd = get_rnd(obj);
@ -887,6 +906,8 @@ random_int(VALUE obj, VALUE vmax)
return add_to_begin(beg, v); return add_to_begin(beg, v);
} }
#define random_int rb_random_int
/* /*
* call-seq: * call-seq:
* prng.float -> float * prng.float -> float