#include "ruby/ruby.h"
#include <limits.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <errno.h>
Go to the source code of this file.
Data Structures | |
struct | MT |
struct | rb_random_t |
Defines | |
#define | N 624 |
#define | M 397 |
#define | MATRIX_A 0x9908b0dfU |
#define | UMASK 0x80000000U |
#define | LMASK 0x7fffffffU |
#define | MIXBITS(u, v) ( ((u) & UMASK) | ((v) & LMASK) ) |
#define | TWIST(u, v) ((MIXBITS(u,v) >> 1) ^ ((v)&1U ? MATRIX_A : 0U)) |
#define | genrand_initialized(mt) ((mt)->next != 0) |
#define | uninit_genrand(mt) ((mt)->next = 0) |
#define | DEFAULT_SEED_CNT 4 |
#define | BDIGITS(x) (RBIGNUM_DIGITS(x)) |
#define | BITSPERDIG (SIZEOF_BDIGITS*CHAR_BIT) |
#define | BIGRAD ((BDIGIT_DBL)1 << BITSPERDIG) |
#define | DIGSPERINT (SIZEOF_INT/SIZEOF_BDIGITS) |
#define | BIGUP(x) ((BDIGIT_DBL)(x) << BITSPERDIG) |
#define | BIGDN(x) RSHIFT(x,BITSPERDIG) |
#define | BIGLO(x) ((BDIGIT)((x) & (BIGRAD-1))) |
#define | BDIGMAX ((BDIGIT)-1) |
#define | roomof(n, m) (int)(((n)+(m)-1) / (m)) |
#define | numberof(array) (int)(sizeof(array) / sizeof((array)[0])) |
#define | SIZEOF_INT32 (31/CHAR_BIT + 1) |
#define | id_minus '-' |
#define | id_plus '+' |
#define | random_free RUBY_TYPED_DEFAULT_FREE |
#define | DEFAULT_SEED_LEN (DEFAULT_SEED_CNT * sizeof(int)) |
#define | USE_DEV_URANDOM 0 |
#define | BIG_GET32(big, i) (RBIGNUM_DIGITS(big)[i]) |
#define | BIG_SET32(big, i, d) (RBIGNUM_DIGITS(big)[i] = (d)) |
Typedefs | |
typedef int | int_must_be_32bit_at_least [sizeof(int)*CHAR_BIT< 32?-1:1] |
Enumerations | |
enum | { MT_MAX_STATE = N } |
Functions | |
static void | init_genrand (struct MT *mt, unsigned int s) |
static void | init_by_array (struct MT *mt, unsigned int init_key[], int key_length) |
static void | next_state (struct MT *mt) |
static unsigned int | genrand_int32 (struct MT *mt) |
static double | genrand_real (struct MT *mt) |
static double | int_pair_to_real_inclusive (unsigned int a, unsigned int b) |
static double | genrand_real2 (struct MT *mt) |
static VALUE | rand_init (struct MT *mt, VALUE vseed) |
static VALUE | random_seed (void) |
static struct MT * | default_mt (void) |
unsigned int | rb_genrand_int32 (void) |
double | rb_genrand_real (void) |
static void | random_mark (void *ptr) |
static size_t | random_memsize (const void *ptr) |
static rb_random_t * | get_rnd (VALUE obj) |
static VALUE | random_alloc (VALUE klass) |
static VALUE | random_init (int argc, VALUE *argv, VALUE obj) |
static void | fill_random_seed (unsigned int seed[DEFAULT_SEED_CNT]) |
static VALUE | make_seed_value (const void *ptr) |
static VALUE | random_get_seed (VALUE obj) |
static VALUE | random_copy (VALUE obj, VALUE orig) |
static VALUE | mt_state (const struct MT *mt) |
static VALUE | random_state (VALUE obj) |
static VALUE | random_s_state (VALUE klass) |
static VALUE | random_left (VALUE obj) |
static VALUE | random_s_left (VALUE klass) |
static VALUE | random_dump (VALUE obj) |
static VALUE | random_load (VALUE obj, VALUE dump) |
static VALUE | rb_f_srand (int argc, VALUE *argv, VALUE obj) |
static unsigned long | make_mask (unsigned long x) |
static unsigned long | limited_rand (struct MT *mt, unsigned long limit) |
static VALUE | limited_big_rand (struct MT *mt, struct RBignum *limit) |
unsigned long | rb_rand_internal (unsigned long i) |
unsigned int | rb_random_int32 (VALUE obj) |
double | rb_random_real (VALUE obj) |
static VALUE | random_bytes (VALUE obj, VALUE len) |
VALUE | rb_random_bytes (VALUE obj, long n) |
static VALUE | range_values (VALUE vmax, VALUE *begp, int *exclp) |
static VALUE | rand_int (struct MT *mt, VALUE vmax, int restrictive) |
static double | float_value (VALUE v) |
static VALUE | random_rand (int argc, VALUE *argv, VALUE obj) |
static VALUE | random_equal (VALUE self, VALUE other) |
static VALUE | rb_f_rand (int argc, VALUE *argv, VALUE obj) |
static VALUE | init_randomseed (struct MT *mt, unsigned int initial[DEFAULT_SEED_CNT]) |
void | Init_RandomSeed (void) |
st_index_t | rb_hash_start (st_index_t h) |
static void | Init_RandomSeed2 (void) |
void | rb_reset_random_seed (void) |
void | Init_Random (void) |
Variables | |
static rb_random_t | default_rand |
VALUE | rb_cRandom |
static const rb_data_type_t | random_data_type |
static st_index_t | hashseed |
#define BDIGITS | ( | x | ) | (RBIGNUM_DIGITS(x)) |
Definition at line 257 of file random.c.
Referenced by int_pair_to_real_inclusive(), and random_load().
#define BIG_GET32 | ( | big, | ||
i | ||||
) | (RBIGNUM_DIGITS(big)[i]) |
Referenced by limited_big_rand().
#define BIG_SET32 | ( | big, | ||
i, | ||||
d | ||||
) | (RBIGNUM_DIGITS(big)[i] = (d)) |
Referenced by limited_big_rand().
#define BIGDN | ( | x | ) | RSHIFT(x,BITSPERDIG) |
Definition at line 262 of file random.c.
Referenced by mt_state().
#define BIGLO | ( | x | ) | ((BDIGIT)((x) & (BIGRAD-1))) |
Definition at line 263 of file random.c.
Referenced by mt_state().
#define BITSPERDIG (SIZEOF_BDIGITS*CHAR_BIT) |
Definition at line 258 of file random.c.
Referenced by int_pair_to_real_inclusive(), and random_load().
#define DEFAULT_SEED_LEN (DEFAULT_SEED_CNT * sizeof(int)) |
Definition at line 460 of file random.c.
Referenced by fill_random_seed(), init_randomseed(), and make_seed_value().
#define DIGSPERINT (SIZEOF_INT/SIZEOF_BDIGITS) |
Definition at line 260 of file random.c.
Referenced by random_load().
#define genrand_initialized | ( | mt | ) | ((mt)->next != 0) |
Definition at line 106 of file random.c.
Referenced by default_mt().
#define id_minus '-' |
Definition at line 322 of file random.c.
Referenced by range_values().
#define id_plus '+' |
Definition at line 323 of file random.c.
Referenced by random_rand().
#define M 397 |
Definition at line 90 of file random.c.
Referenced by next_state().
#define N 624 |
Definition at line 89 of file random.c.
Referenced by init_by_array(), and next_state().
#define numberof | ( | array | ) | (int)(sizeof(array) / sizeof((array)[0])) |
Definition at line 267 of file random.c.
Referenced by mt_state(), rand_init(), random_copy(), and random_load().
#define roomof | ( | n, | ||
m | ||||
) | (int)(((n)+(m)-1) / (m)) |
Definition at line 266 of file random.c.
Referenced by int_pair_to_real_inclusive(), rand_init(), and random_load().
#define SIZEOF_INT32 (31/CHAR_BIT + 1) |
Definition at line 268 of file random.c.
Referenced by limited_rand(), mt_state(), and rand_init().
#define TWIST | ( | u, | ||
v | ||||
) | ((MIXBITS(u,v) >> 1) ^ ((v)&1U ? MATRIX_A : 0U)) |
Definition at line 95 of file random.c.
Referenced by next_state().
#define uninit_genrand | ( | mt | ) | ((mt)->next = 0) |
Definition at line 107 of file random.c.
Referenced by rb_reset_random_seed().
typedef int int_must_be_32bit_at_least[sizeof(int)*CHAR_BIT< 32?-1:1] |
static struct MT* default_mt | ( | void | ) | [static, read] |
Definition at line 233 of file random.c.
References genrand_initialized, rb_random_t::mt, rand_init(), random_seed(), and rb_random_t::seed.
Referenced by rb_f_rand(), rb_genrand_int32(), rb_genrand_real(), and rb_rand_internal().
static void fill_random_seed | ( | unsigned int | seed[DEFAULT_SEED_CNT] | ) | [static] |
Definition at line 469 of file random.c.
References CHAR_BIT, DEFAULT_SEED_LEN, fstat, gettimeofday(), NULL, O_NONBLOCK, S_ISCHR, stat, timeval::tv_sec, and timeval::tv_usec.
Referenced by init_randomseed(), and random_seed().
static double float_value | ( | VALUE | v | ) | [inline, static] |
Definition at line 963 of file random.c.
References INT2FIX, isinf(), isnan, rb_class_new_instance(), rb_eSystemCallError, rb_exc_raise(), and RFLOAT_VALUE.
Referenced by random_rand().
static unsigned int genrand_int32 | ( | struct MT * | mt | ) | [static] |
Definition at line 177 of file random.c.
References MT::left, MT::next, and next_state().
Referenced by genrand_real(), genrand_real2(), Init_RandomSeed(), limited_big_rand(), limited_rand(), rb_genrand_int32(), rb_random_bytes(), and rb_random_int32().
static double genrand_real | ( | struct MT * | mt | ) | [static] |
Definition at line 196 of file random.c.
References genrand_int32().
Referenced by random_rand(), rb_f_rand(), rb_genrand_real(), and rb_random_real().
static double genrand_real2 | ( | struct MT * | mt | ) | [static] |
Definition at line 206 of file random.c.
References genrand_int32(), and int_pair_to_real_inclusive().
Referenced by random_rand().
static rb_random_t* get_rnd | ( | VALUE | obj | ) | [static] |
Definition at line 348 of file random.c.
References TypedData_Get_Struct.
Referenced by random_copy(), random_dump(), random_equal(), random_get_seed(), random_init(), random_left(), random_load(), random_rand(), random_state(), rb_random_bytes(), rb_random_int32(), and rb_random_real().
static void init_by_array | ( | struct MT * | mt, | |
unsigned int | init_key[], | |||
int | key_length | |||
) | [static] |
Definition at line 132 of file random.c.
References init_genrand(), N, and MT::state.
Referenced by init_randomseed(), and rand_init().
static void init_genrand | ( | struct MT * | mt, | |
unsigned int | s | |||
) | [static] |
Definition at line 111 of file random.c.
References MT::left, MT::next, and MT::state.
Referenced by init_by_array(), and rand_init().
void Init_Random | ( | void | ) |
Definition at line 1213 of file random.c.
References CLASS_OF, Init_RandomSeed2(), random_alloc(), random_bytes(), random_copy(), random_dump(), random_equal(), random_get_seed(), random_init(), random_left(), random_load(), random_rand(), random_s_left(), random_s_state(), random_seed(), random_state(), rb_cObject, rb_cRandom, rb_define_alloc_func(), rb_define_class(), rb_define_global_function(), rb_define_method(), rb_define_private_method(), rb_define_singleton_method(), rb_f_rand(), and rb_f_srand().
Definition at line 1152 of file random.c.
References DEFAULT_SEED_LEN, fill_random_seed(), init_by_array(), and make_seed_value().
Referenced by Init_RandomSeed().
void Init_RandomSeed | ( | void | ) |
Definition at line 1163 of file random.c.
References genrand_int32(), hashseed, init_randomseed(), rb_random_t::mt, rb_global_variable(), and rb_random_t::seed.
static void Init_RandomSeed2 | ( | void | ) | [static] |
Definition at line 1195 of file random.c.
References rb_cBignum, RB_TYPE_P, RBASIC, rb_random_t::seed, and T_BIGNUM.
Referenced by Init_Random().
static double int_pair_to_real_inclusive | ( | unsigned int | a, | |
unsigned int | b | |||
) | [static] |
Definition at line 271 of file random.c.
References BDIGIT, BDIGITS, BITSPERDIG, FIX2ULONG, FIXNUM_P, INT2FIX, len, MEMMOVE, MEMZERO, rb_big2dbl(), rb_big_mul(), rb_big_new(), rb_big_rshift(), RBIGNUM_LEN, and roomof.
Referenced by genrand_real2().
Definition at line 803 of file random.c.
References BDIGIT, BIG_GET32, BIG_SET32, genrand_int32(), len, make_mask(), rb_big_clone(), rb_big_norm(), RBIGNUM_LEN, RBIGNUM_SET_SIGN, and SIZEOF_BDIGITS.
Referenced by rand_int(), and random_rand().
static unsigned long limited_rand | ( | struct MT * | mt, | |
unsigned long | limit | |||
) | [static] |
Definition at line 781 of file random.c.
References genrand_int32(), make_mask(), and SIZEOF_INT32.
Referenced by rand_int(), random_rand(), and rb_rand_internal().
static unsigned long make_mask | ( | unsigned long | x | ) | [static] |
Definition at line 767 of file random.c.
Referenced by limited_big_rand(), and limited_rand().
static VALUE make_seed_value | ( | const void * | ptr | ) | [static] |
Definition at line 517 of file random.c.
References BDIGIT, DEFAULT_SEED_LEN, len, MEMCPY, NEWOBJ, OBJSETUP, rb_big_norm(), rb_big_resize(), rb_cBignum, RBIGNUM_DIGITS, RBIGNUM_SET_SIGN, and T_BIGNUM.
Referenced by init_randomseed(), and random_seed().
Definition at line 580 of file random.c.
References BDIGIT, BIGDN, BIGLO, numberof, rb_big_new(), rb_big_norm(), RBIGNUM_DIGITS, SIZEOF_INT32, and MT::state.
Referenced by random_dump(), random_s_state(), and random_state().
static void next_state | ( | struct MT * | mt | ) | [static] |
Definition at line 366 of file random.c.
References ALLOC_N, buf, FIX2LONG, init_by_array(), init_genrand(), len, MT_MAX_STATE, numberof, rb_eTypeError, rb_obj_classname(), rb_raise(), rb_to_int(), RBIGNUM_DIGITS, RBIGNUM_LEN, roomof, SIZEOF_BDIGITS, SIZEOF_INT32, T_BIGNUM, T_FIXNUM, TYPE, and xfree().
Referenced by default_mt(), random_init(), and rb_f_srand().
Definition at line 925 of file random.c.
References FIX2LONG, FIXNUM_P, INT2FIX, limited_big_rand(), limited_rand(), LONG2NUM, rb_big_clone(), rb_big_minus(), rb_bigzero_p(), RB_GC_GUARD, RBIGNUM, RBIGNUM_SET_SIGN, RBIGNUM_SIGN, and ULONG2NUM().
Referenced by random_rand(), and rb_f_rand().
Definition at line 357 of file random.c.
References INT2FIX, rb_random_t::seed, and TypedData_Make_Struct.
Referenced by Init_Random().
Definition at line 881 of file random.c.
References NUM2LONG(), rb_random_bytes(), and rb_to_int().
Referenced by Init_Random().
Definition at line 568 of file random.c.
References get_rnd(), MT::left, rb_random_t::mt, MT::next, numberof, and MT::state.
Referenced by Init_Random().
Definition at line 633 of file random.c.
References get_rnd(), INT2FIX, MT::left, rb_random_t::mt, mt_state(), rb_ary_new2(), rb_ary_push(), and rb_random_t::seed.
Referenced by Init_Random().
Definition at line 1099 of file random.c.
References get_rnd(), MT::left, memcmp(), rb_random_t::mt, MT::next, rb_funcall2(), rb_intern, rb_obj_class(), RTEST, rb_random_t::seed, and MT::state.
Referenced by Init_Random().
Definition at line 561 of file random.c.
References get_rnd(), and rb_random_t::seed.
Referenced by Init_Random().
Definition at line 445 of file random.c.
References get_rnd(), rb_random_t::mt, rand_init(), random_seed(), rb_scan_args(), and rb_random_t::seed.
Referenced by Init_Random().
Definition at line 618 of file random.c.
References get_rnd(), INT2FIX, MT::left, and rb_random_t::mt.
Referenced by Init_Random().
Definition at line 647 of file random.c.
References BDIGIT, BDIGITS, BITSPERDIG, Check_Type, DIGSPERINT, FIX2ULONG, FIXNUM_P, get_rnd(), INT2FIX, MT::left, len, rb_random_t::mt, MT::next, NUM2ULONG, numberof, RARRAY_LEN, RARRAY_PTR, rb_eArgError, rb_raise(), rb_to_int(), RBIGNUM_DIGITS, RBIGNUM_LEN, roomof, rb_random_t::seed, SIZEOF_BDIGITS, MT::state, T_ARRAY, and T_BIGNUM.
Referenced by Init_Random().
static void random_mark | ( | void * | ptr | ) | [static] |
Definition at line 327 of file random.c.
References rb_gc_mark().
Definition at line 998 of file random.c.
References BUILTIN_TYPE, FIX2LONG, FIXNUM_P, float_value(), genrand_real(), genrand_real2(), get_rnd(), id_plus, INT2FIX, limited_big_rand(), limited_rand(), LONG2NUM, max(), rb_random_t::mt, NIL_P, NUM2LONG(), Qfalse, Qundef, rand_int(), range_values(), rb_big_minus(), rb_big_norm(), rb_big_plus(), rb_bigzero_p(), rb_check_to_float(), rb_check_to_integer(), rb_eArgError, rb_exc_new3(), rb_exc_raise(), rb_float_new(), rb_funcall2(), rb_obj_as_string(), rb_raise(), rb_str_append(), rb_str_new_cstr(), RBIGNUM, RBIGNUM_SIGN, RFLOAT_VALUE, T_BIGNUM, T_FLOAT, TYPE, and ULONG2NUM().
Referenced by Init_Random().
Definition at line 626 of file random.c.
References INT2FIX, MT::left, and rb_random_t::mt.
Referenced by Init_Random().
Definition at line 611 of file random.c.
References rb_random_t::mt, and mt_state().
Referenced by Init_Random().
static VALUE random_seed | ( | void | ) | [static] |
Definition at line 548 of file random.c.
References buf, fill_random_seed(), and make_seed_value().
Referenced by default_mt(), Init_Random(), random_init(), and rb_f_srand().
Definition at line 603 of file random.c.
References get_rnd(), rb_random_t::mt, and mt_state().
Referenced by Init_Random().
Definition at line 913 of file random.c.
References id_minus, NIL_P, rb_funcall2(), rb_range_values(), and rb_respond_to().
Referenced by random_rand().
Definition at line 1133 of file random.c.
References DBL2NUM, default_mt(), genrand_real(), INT2FIX, NIL_P, rand_int(), rb_scan_args(), and rb_to_int().
Referenced by Init_Random().
Definition at line 748 of file random.c.
References rb_random_t::mt, rand_init(), random_seed(), rb_scan_args(), rb_secure(), and rb_random_t::seed.
Referenced by Init_Random().
unsigned int rb_genrand_int32 | ( | void | ) |
Definition at line 244 of file random.c.
References default_mt(), and genrand_int32().
double rb_genrand_real | ( | void | ) |
Definition at line 251 of file random.c.
References default_mt(), and genrand_real().
Referenced by rb_ary_sample(), and rb_ary_shuffle_bang().
st_index_t rb_hash_start | ( | st_index_t | h | ) |
Definition at line 1189 of file random.c.
References hashseed, and st_hash_start.
Referenced by hash_i(), match_hash(), method_hash(), proc_hash(), rb_any_hash(), rb_memhash(), rb_obj_hash(), and recursive_hash().
unsigned long rb_rand_internal | ( | unsigned long | i | ) |
Definition at line 854 of file random.c.
References default_mt(), and limited_rand().
Referenced by big_sparse_p().
Definition at line 887 of file random.c.
References genrand_int32(), get_rnd(), rb_random_t::mt, rb_str_new(), and RSTRING_PTR.
Referenced by random_bytes().
unsigned int rb_random_int32 | ( | VALUE | obj | ) |
Definition at line 861 of file random.c.
References genrand_int32(), get_rnd(), and rb_random_t::mt.
double rb_random_real | ( | VALUE | obj | ) |
Definition at line 868 of file random.c.
References genrand_real(), get_rnd(), and rb_random_t::mt.
void rb_reset_random_seed | ( | void | ) |
Definition at line 1205 of file random.c.
References INT2FIX, rb_random_t::mt, rb_random_t::seed, and uninit_genrand.
Referenced by rb_thread_atfork().
rb_random_t default_rand [static] |
st_index_t hashseed [static] |
Definition at line 1149 of file random.c.
Referenced by Init_RandomSeed(), and rb_hash_start().
const rb_data_type_t random_data_type [static] |