Ruby  1.9.3p551(2014-11-13revision48407)
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
random.c File Reference
#include "ruby/ruby.h"
#include <limits.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <errno.h>
#include "siphash.c"

Go to the source code of this file.

Data Structures

struct  MT
 
struct  rb_random_t
 

Macros

#define N   624
 
#define M   397
 
#define MATRIX_A   0x9908b0dfU /* constant vector a */
 
#define UMASK   0x80000000U /* most significant w-r bits */
 
#define LMASK   0x7fffffffU /* least significant r bits */
 
#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 DEFAULT_SEED_LEN   (DEFAULT_SEED_CNT * (int)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))
 
#define SIP_HASH_STREAMING   0
 
#define sip_hash24   ruby_sip_hash24
 
#define BYTE_ORDER   LITTLE_ENDIAN
 
#define LITTLE_ENDIAN   1234
 
#define BIG_ENDIAN   4321
 

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 rb_random_trand_start (rb_random_t *r)
 
static struct MTdefault_mt (void)
 
unsigned int rb_genrand_int32 (void)
 
double rb_genrand_real (void)
 
static void random_mark (void *ptr)
 
static void random_free (void *ptr)
 
static size_t random_memsize (const void *ptr)
 
static rb_random_tget_rnd (VALUE obj)
 
static rb_random_ttry_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_genrand_ulong_limited (unsigned long limit)
 
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, VALUE *endp, int *exclp)
 
static VALUE rand_int (struct MT *mt, VALUE vmax, int restrictive)
 
static double float_value (VALUE v)
 
static VALUE rand_range (struct MT *mt, VALUE range)
 
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 random_s_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)
 
st_index_t rb_memhash (const void *ptr, long len)
 
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 VALUE rb_Random_DEFAULT
 
static ID id_rand
 
static ID id_bytes
 
static const rb_data_type_t random_data_type
 
static st_index_t hashseed
 
union {
   uint8_t   key [16]
 
   uint32_t   u32 [(16 *sizeof(uint8_t)-1)/sizeof(uint32_t)]
 
sipseed
 

Macro Definition Documentation

#define BDIGITS (   x)    (RBIGNUM_DIGITS(x))

Definition at line 265 of file random.c.

Referenced by int_pair_to_real_inclusive(), and random_load().

#define BDIGMAX   ((BDIGIT)-1)

Definition at line 272 of file random.c.

#define BIG_ENDIAN   4321

Definition at line 1277 of file random.c.

#define BIG_GET32 (   big,
  i 
)    (RBIGNUM_DIGITS(big)[(i)])

Referenced by limited_big_rand().

#define BIG_SET32 (   big,
  i,
 
)    (RBIGNUM_DIGITS(big)[(i)] = (d))

Referenced by limited_big_rand().

#define BIGDN (   x)    RSHIFT((x),BITSPERDIG)

Definition at line 270 of file random.c.

Referenced by mt_state().

#define BIGLO (   x)    ((BDIGIT)((x) & (BIGRAD-1)))

Definition at line 271 of file random.c.

Referenced by mt_state().

#define BIGRAD   ((BDIGIT_DBL)1 << BITSPERDIG)

Definition at line 267 of file random.c.

#define BIGUP (   x)    ((BDIGIT_DBL)(x) << BITSPERDIG)

Definition at line 269 of file random.c.

#define BITSPERDIG   (SIZEOF_BDIGITS*CHAR_BIT)

Definition at line 266 of file random.c.

Referenced by int_pair_to_real_inclusive(), rand_init(), and random_load().

#define BYTE_ORDER   LITTLE_ENDIAN

Definition at line 1271 of file random.c.

Referenced by SHA256_Transform(), and SHA512_Transform().

#define DEFAULT_SEED_CNT   4

Definition at line 228 of file random.c.

Referenced by Init_RandomSeed(), and random_seed().

#define DEFAULT_SEED_LEN   (DEFAULT_SEED_CNT * (int)sizeof(int))

Definition at line 487 of file random.c.

Referenced by fill_random_seed(), init_randomseed(), and make_seed_value().

#define DIGSPERINT   (SIZEOF_INT/SIZEOF_BDIGITS)

Definition at line 268 of file random.c.

Referenced by random_load().

#define genrand_initialized (   mt)    ((mt)->next != 0)

Definition at line 109 of file random.c.

Referenced by rand_start().

#define id_minus   '-'

Definition at line 331 of file random.c.

Referenced by range_values().

#define id_plus   '+'

Definition at line 332 of file random.c.

Referenced by rand_range().

#define LITTLE_ENDIAN   1234

Definition at line 1274 of file random.c.

Referenced by SHA256_Transform(), and SHA512_Transform().

#define LMASK   0x7fffffffU /* least significant r bits */

Definition at line 96 of file random.c.

#define M   397

Definition at line 93 of file random.c.

Referenced by next_state().

#define MATRIX_A   0x9908b0dfU /* constant vector a */

Definition at line 94 of file random.c.

#define MIXBITS (   u,
  v 
)    ( ((u) & UMASK) | ((v) & LMASK) )

Definition at line 97 of file random.c.

#define N   624

Definition at line 92 of file random.c.

Referenced by init_by_array(), init_genrand(), and next_state().

#define numberof (   array)    (int)(sizeof(array) / sizeof((array)[0]))

Definition at line 275 of file random.c.

Referenced by Init_RandomSeed(), mt_state(), rand_init(), random_copy(), and random_load().

#define roomof (   n,
 
)    (int)(((n)+(m)-1) / (m))

Definition at line 274 of file random.c.

Referenced by int_pair_to_real_inclusive(), rand_init(), and random_load().

#define sip_hash24   ruby_sip_hash24

Definition at line 1266 of file random.c.

Referenced by rb_memhash().

#define SIP_HASH_STREAMING   0

Definition at line 1265 of file random.c.

#define SIZEOF_INT32   (31/CHAR_BIT + 1)

Definition at line 276 of file random.c.

Referenced by limited_rand(), mt_state(), rand_init(), and rb_random_bytes().

#define TWIST (   u,
  v 
)    ((MIXBITS((u),(v)) >> 1) ^ ((v)&1U ? MATRIX_A : 0U))

Definition at line 98 of file random.c.

Referenced by next_state().

#define UMASK   0x80000000U /* most significant w-r bits */

Definition at line 95 of file random.c.

#define uninit_genrand (   mt)    ((mt)->next = 0)

Definition at line 110 of file random.c.

Referenced by rb_reset_random_seed().

#define USE_DEV_URANDOM   0

Definition at line 492 of file random.c.

Typedef Documentation

typedef int int_must_be_32bit_at_least[sizeof(int)*CHAR_BIT< 32?-1:1]

Definition at line 89 of file random.c.

Enumeration Type Documentation

anonymous enum
Enumerator
MT_MAX_STATE 

Definition at line 100 of file random.c.

Function Documentation

static struct MT* default_mt ( void  )
staticread

Definition at line 246 of file random.c.

References rb_random_t::mt, and rand_start().

Referenced by rb_f_rand(), rb_genrand_int32(), rb_genrand_real(), and rb_genrand_ulong_limited().

static void fill_random_seed ( unsigned int  seed[DEFAULT_SEED_CNT])
static
static double float_value ( VALUE  v)
inlinestatic
static unsigned int genrand_int32 ( struct MT mt)
static
static double genrand_real ( struct MT mt)
static

Definition at line 199 of file random.c.

References genrand_int32().

Referenced by rand_range(), random_rand(), rb_f_rand(), rb_genrand_real(), and rb_random_real().

static double genrand_real2 ( struct MT mt)
static

Definition at line 209 of file random.c.

References genrand_int32(), and int_pair_to_real_inclusive().

Referenced by rand_range().

static rb_random_t* get_rnd ( VALUE  obj)
static
static void init_by_array ( struct MT mt,
unsigned int  init_key[],
int  key_length 
)
static

Definition at line 135 of file random.c.

References i, 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 114 of file random.c.

References MT::left, N, MT::next, and MT::state.

Referenced by init_by_array(), and rand_init().

void Init_Random ( void  )
static VALUE init_randomseed ( struct MT mt,
unsigned int  initial[DEFAULT_SEED_CNT] 
)
static

Definition at line 1289 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  )
static void Init_RandomSeed2 ( void  )
static

Definition at line 1347 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
static VALUE limited_big_rand ( struct MT mt,
struct RBignum limit 
)
static
static unsigned long limited_rand ( struct MT mt,
unsigned long  limit 
)
static

Definition at line 811 of file random.c.

References genrand_int32(), i, long, make_mask(), and SIZEOF_INT32.

Referenced by rand_int(), rand_range(), and rb_genrand_ulong_limited().

static unsigned long make_mask ( unsigned long  x)
static

Definition at line 797 of file random.c.

Referenced by limited_big_rand(), and limited_rand().

static VALUE make_seed_value ( const void *  ptr)
static
static VALUE mt_state ( const struct MT mt)
static
static void next_state ( struct MT mt)
static

Definition at line 161 of file random.c.

References MT::left, M, N, MT::next, p, MT::state, and TWIST.

Referenced by genrand_int32().

static VALUE rand_init ( struct MT mt,
VALUE  vseed 
)
static
static VALUE rand_int ( struct MT mt,
VALUE  vmax,
int  restrictive 
)
static
static VALUE rand_range ( struct MT mt,
VALUE  range 
)
inlinestatic
static rb_random_t* rand_start ( rb_random_t r)
static
static VALUE random_alloc ( VALUE  klass)
static

Definition at line 384 of file random.c.

References INT2FIX, rb_random_t::seed, and TypedData_Make_Struct.

Referenced by Init_Random().

static VALUE random_bytes ( VALUE  obj,
VALUE  len 
)
static

Definition at line 934 of file random.c.

References NUM2LONG(), rb_random_bytes(), and rb_to_int().

Referenced by Init_Random().

static VALUE random_copy ( VALUE  obj,
VALUE  orig 
)
static

Definition at line 598 of file random.c.

References get_rnd(), MT::left, rb_random_t::mt, MT::next, numberof, and MT::state.

Referenced by Init_Random().

static VALUE random_dump ( VALUE  obj)
static

Definition at line 663 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().

static VALUE random_equal ( VALUE  self,
VALUE  other 
)
static
static void random_free ( void *  ptr)
static

Definition at line 343 of file random.c.

References xfree().

static VALUE random_get_seed ( VALUE  obj)
static

Definition at line 591 of file random.c.

References get_rnd(), and rb_random_t::seed.

Referenced by Init_Random().

static VALUE random_init ( int  argc,
VALUE argv,
VALUE  obj 
)
static

Definition at line 472 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().

static VALUE random_left ( VALUE  obj)
static

Definition at line 648 of file random.c.

References get_rnd(), INT2FIX, MT::left, and rb_random_t::mt.

Referenced by Init_Random().

static VALUE random_load ( VALUE  obj,
VALUE  dump 
)
static
static void random_mark ( void *  ptr)
static

Definition at line 337 of file random.c.

References rb_gc_mark().

static size_t random_memsize ( const void *  ptr)
static

Definition at line 350 of file random.c.

static VALUE random_rand ( int  argc,
VALUE argv,
VALUE  obj 
)
static
static VALUE random_s_left ( VALUE  klass)
static

Definition at line 656 of file random.c.

References INT2FIX, MT::left, and rb_random_t::mt.

Referenced by Init_Random().

static VALUE random_s_rand ( int  argc,
VALUE argv,
VALUE  obj 
)
static

Definition at line 1259 of file random.c.

References rand_start(), random_rand(), and rb_Random_DEFAULT.

Referenced by Init_Random().

static VALUE random_s_state ( VALUE  klass)
static

Definition at line 641 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 578 of file random.c.

References buf, DEFAULT_SEED_CNT, fill_random_seed(), and make_seed_value().

Referenced by Init_Random(), rand_start(), random_init(), and rb_f_srand().

static VALUE random_state ( VALUE  obj)
static

Definition at line 633 of file random.c.

References get_rnd(), rb_random_t::mt, and mt_state().

Referenced by Init_Random().

static VALUE range_values ( VALUE  vmax,
VALUE begp,
VALUE endp,
int *  exclp 
)
static

Definition at line 972 of file random.c.

References id_minus, NIL_P, Qfalse, rb_funcall2(), rb_range_values(), and rb_respond_to().

Referenced by rand_range().

static VALUE rb_f_rand ( int  argc,
VALUE argv,
VALUE  obj 
)
static

Definition at line 1230 of file random.c.

References DBL2NUM, default_mt(), genrand_real(), INT2FIX, NIL_P, Qfalse, rand_int(), rand_range(), rb_scan_args(), rb_to_int(), and v.

Referenced by Init_Random().

static VALUE rb_f_srand ( int  argc,
VALUE argv,
VALUE  obj 
)
static
unsigned int rb_genrand_int32 ( void  )

Definition at line 252 of file random.c.

References default_mt(), and genrand_int32().

double rb_genrand_real ( void  )

Definition at line 259 of file random.c.

References default_mt(), and genrand_real().

unsigned long rb_genrand_ulong_limited ( unsigned long  limit)

Definition at line 890 of file random.c.

References default_mt(), and limited_rand().

Referenced by big_sparse_p().

st_index_t rb_hash_start ( st_index_t  h)

Definition at line 1330 of file random.c.

References hashseed, and st_hash_start.

Referenced by match_hash(), method_hash(), proc_hash(), rb_any_hash(), rb_obj_hash(), and recursive_hash().

st_index_t rb_memhash ( const void *  ptr,
long  len 
)
VALUE rb_random_bytes ( VALUE  obj,
long  n 
)
unsigned int rb_random_int32 ( VALUE  obj)
double rb_random_real ( VALUE  obj)

Definition at line 913 of file random.c.

References genrand_real(), id_rand, rb_random_t::mt, NUM2DBL, rb_eRangeError, rb_funcall2(), rb_raise(), try_get_rnd(), and v.

Referenced by rb_ary_sample().

void rb_reset_random_seed ( void  )

Definition at line 1357 of file random.c.

References default_rand, INT2FIX, rb_random_t::mt, rb_random_t::seed, and uninit_genrand.

Referenced by rb_thread_atfork().

static rb_random_t* try_get_rnd ( VALUE  obj)
static

Definition at line 373 of file random.c.

References DATA_PTR, NULL, rand_start(), rb_cRandom, and rb_typeddata_is_kind_of().

Referenced by rb_random_bytes(), rb_random_int32(), and rb_random_real().

Variable Documentation

rb_random_t default_rand
static

Definition at line 230 of file random.c.

Referenced by Init_RandomSeed(), rb_f_srand(), and rb_reset_random_seed().

st_index_t hashseed
static

Definition at line 1282 of file random.c.

Referenced by Init_RandomSeed(), and rb_hash_start().

ID id_bytes
static

Definition at line 333 of file random.c.

Referenced by Init_Random(), and rb_random_bytes().

ID id_rand
static

Definition at line 333 of file random.c.

Referenced by Init_Random(), rb_random_int32(), and rb_random_real().

uint8_t key[16]

Definition at line 1284 of file random.c.

Referenced by cbsubst_get_subst_key(), check_deadlock_i(), check_exec_env_i(), check_exec_options_i(), cto_i(), env_fetch(), env_shift(), fc_i(), fdbm_clear(), fdbm_delete(), fdbm_delete_if(), fdbm_each_key(), fdbm_each_pair(), fdbm_each_value(), fdbm_empty_p(), fdbm_fetch(), fdbm_has_key(), fdbm_has_value(), fdbm_invert(), fdbm_key(), fdbm_keys(), fdbm_length(), fdbm_select(), fdbm_shift(), fdbm_store(), fdbm_to_a(), fdbm_to_hash(), fdbm_values(), fgdbm_clear(), fgdbm_empty_p(), fgdbm_has_key(), fgdbm_length(), fgdbm_store(), fgdbm_values(), force_chain_object(), fsdbm_clear(), fsdbm_delete(), fsdbm_delete_if(), fsdbm_each_key(), fsdbm_each_pair(), fsdbm_each_value(), fsdbm_empty_p(), fsdbm_fetch(), fsdbm_has_key(), fsdbm_has_value(), fsdbm_invert(), fsdbm_key(), fsdbm_keys(), fsdbm_length(), fsdbm_select(), fsdbm_shift(), fsdbm_store(), fsdbm_to_a(), fsdbm_to_hash(), fsdbm_values(), generate_json_object(), generic_ivar_remove(), getnext(), getnkey(), hash2named_arg(), hash2ptr_dispparams(), hash_foreach_iter(), if(), iseq_compile_each(), key_i(), load_unlock(), map_charset(), obj_ivar_each(), oletypelib_path(), onig_st_insert_strend(), onig_st_lookup_strend(), ossl_call_client_cert_cb(), ossl_cipher_init(), ossl_cipher_initialize(), ossl_cipher_pkcs5_keyivgen(), ossl_cipher_set_key(), ossl_pkcs12_initialize(), ossl_pkcs12_s_create(), ossl_pkcs7_decrypt(), ossl_pkcs7_s_sign(), ossl_spki_set_public_key(), ossl_sslctx_setup(), ossl_x509_set_public_key(), ossl_x509req_set_public_key(), parse_exp(), r_leave(), r_object0(), rb_enc_aliases_enc_i(), rb_free_generic_ivar(), rb_gdbm_delete(), rb_gdbm_fetch2(), rb_gdbm_firstkey(), rb_gdbm_nextkey(), rb_hash_default(), rb_hash_fetch_m(), rb_hash_s_create(), rb_run_exec_options_err(), rb_w32_close(), remove_method(), run_final(), set_threads_event_flags_i(), shift_i_safe(), splpage(), st_foreach(), st_insert2(), syck_map_add(), syck_map_initialize(), syck_map_value_set(), terminate_atfork_before_exec_i(), terminate_atfork_i(), terminate_i(), tr_setup_table(), and update_char_offset().

const rb_data_type_t random_data_type
static
Initial value:
= {
"random",
{
},
}

Definition at line 355 of file random.c.

VALUE rb_cRandom

Definition at line 329 of file random.c.

VALUE rb_Random_DEFAULT
static

Definition at line 330 of file random.c.

Referenced by Init_Random(), and random_s_rand().

union { ... } sipseed

Referenced by Init_RandomSeed(), and rb_memhash().

Definition at line 1285 of file random.c.