Ruby: random.c File Reference

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

Data Structures | Macros | Typedefs | Functions | Variables
random.c File Reference
`#include "ruby/internal/config.h"`
`#include <errno.h>`
`#include <limits.h>`
`#include <math.h>`
`#include <float.h>`
`#include <time.h>`
`#include <sys/types.h>`
`#include <sys/stat.h>`
`#include "internal.h"`
`#include "internal/array.h"`
`#include "internal/compilers.h"`
`#include "internal/numeric.h"`
`#include "internal/random.h"`
`#include "internal/sanitizers.h"`
`#include "internal/variable.h"`
`#include "ruby_atomic.h"`
`#include "ruby/random.h"`
`#include "ruby/ractor.h"`
`#include "missing/mt19937.c"`
`#include "siphash.c"`

Go to the source code of this file.

## Data Structures
struct | rb_random_mt_t
struct | hash_salt_t

## Macros
#define | DEFAULT_SEED_CNT 4
#define | SIZEOF_INT32 (31/CHAR_BIT + 1)
#define | id_minus '-'
#define | id_plus '+'
#define | random_mark rb_random_mark
#define | random_free RUBY_TYPED_DEFAULT_FREE
#define | random_mt_mark rb_random_mark
#define | DEFAULT_SEED_LEN (DEFAULT_SEED_CNT * (int)sizeof(int32_t))
#define | USE_DEV_URANDOM 0
#define | fill_random_bytes_urandom(seed, size) -1
#define | fill_random_bytes_syscall(seed, size, need_secure) -1
#define | fill_random_bytes ruby_fill_random_bytes
#define | with_random_seed(size, add)
#define | SIP_HASH_STREAMING 0
#define | sip_hash13 ruby_sip_hash13
#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]

## Functions
| RB_RANDOM_INTERFACE_DECLARE (rand_mt)
unsigned int | rb_genrand_int32 (void)
double | rb_genrand_real (void)
void | random_mark (void *ptr)
void | rb_random_base_init (rb_random_t *rnd)
int | ruby_fill_random_bytes (void *seed, size_t size, int need_secure)
unsigned long | rb_genrand_ulong_limited (unsigned long limit)
unsigned int | rb_random_int32 (VALUE obj)
double | rb_int_pair_to_real (uint32_t a, uint32_t b, int excl)
double | rb_random_real (VALUE obj)
unsigned long | rb_random_ulong_limited (VALUE obj, unsigned long limit)
void | rb_rand_bytes_int32 (rb_random_get_int32_func *get_int32, rb_random_t *rnd, void *p, size_t n)
VALUE | rb_random_bytes (VALUE obj, long n)
| NO_SANITIZE ("unsigned-integer-overflow", extern st_index_t rb_hash_start(st_index_t h))
st_index_t | rb_hash_start (st_index_t h)
st_index_t | rb_memhash (const void *ptr, long len)
void | Init_RandomSeedCore (void)
void | rb_reset_random_seed (void)
void | InitVM_Random (void)
void | Init_Random (void)

## Variables
VALUE | rb_cRandom
const rb_data_type_t | rb_random_data_type

## Macro Definition Documentation

## ◆ BIG_ENDIAN

#define BIG_ENDIAN 4321

Definition at line 1597 of file random.c.

## ◆ BYTE_ORDER

#define BYTE_ORDER LITTLE_ENDIAN

Definition at line 1591 of file random.c.

## ◆ DEFAULT_SEED_CNT

#define DEFAULT_SEED_CNT 4

Definition at line 127 of file random.c.

## ◆ DEFAULT_SEED_LEN

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

Definition at line 418 of file random.c.

## ◆ fill_random_bytes

#define fill_random_bytes ruby_fill_random_bytes

Definition at line 582 of file random.c.

## ◆ fill_random_bytes_syscall

#define fill_random_bytes_syscall | ( |  | seed,
|  |  | size,
|  |  | need_secure
| ) |  | -1

Definition at line 571 of file random.c.

## ◆ fill_random_bytes_urandom

#define fill_random_bytes_urandom | ( |  | seed,
|  |  | size
| ) |  | -1

Definition at line 463 of file random.c.

## ◆ id_minus

#define id_minus '-'

Definition at line 227 of file random.c.

## ◆ id_plus

#define id_plus '+'

Definition at line 228 of file random.c.

## ◆ LITTLE_ENDIAN

#define LITTLE_ENDIAN 1234

Definition at line 1594 of file random.c.

## ◆ random_free

#define random_free RUBY_TYPED_DEFAULT_FREE

Definition at line 241 of file random.c.

## ◆ random_mark

#define random_mark rb_random_mark

Definition at line 233 of file random.c.

## ◆ random_mt_mark

#define random_mt_mark rb_random_mark

Definition at line 259 of file random.c.

## ◆ sip_hash13

#define sip_hash13 ruby_sip_hash13

Definition at line 1586 of file random.c.

## ◆ SIP_HASH_STREAMING

#define SIP_HASH_STREAMING 0

Definition at line 1585 of file random.c.

## ◆ SIZEOF_INT32

#define SIZEOF_INT32 (31/CHAR_BIT + 1)

Definition at line 202 of file random.c.

## ◆ USE_DEV_URANDOM

#define USE_DEV_URANDOM 0

Definition at line 423 of file random.c.

## ◆ with_random_seed

#define with_random_seed | ( |  | size,
|  |  | add
| ) |  |

Value:
for (uint32_t seedbuf[(size)+(add)], loop = (fill_random_seed(seedbuf, (size)), 1); \
loop; explicit_bzero(seedbuf, (size)*sizeof(seedbuf[0])), loop = 0)
add
#define add(x, y)
Definition: date_strftime.c:23
explicit_bzero
void explicit_bzero(void *b, size_t len)
Definition: explicit_bzero.c:77
size
voidpf void uLong size
Definition: ioapi.h:138
uint32_t
unsigned int uint32_t
Definition: sha2.h:101

Definition at line 634 of file random.c.

## Typedef Documentation

## ◆ int_must_be_32bit_at_least

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

Definition at line 73 of file random.c.

## Function Documentation

## ◆ Init_Random()

void Init_Random | ( | void |  | ) |

Definition at line 1754 of file random.c.

References InitVM, and rb_intern().

## ◆ Init_RandomSeedCore()

void Init_RandomSeedCore | ( | void |  | ) |

Definition at line 1642 of file random.c.

References DEFAULT_SEED_CNT, explicit_bzero(), FALSE, fill_random_bytes, and with_random_seed.

## ◆ InitVM_Random()

void InitVM_Random | ( | void |  | ) |

Definition at line 1697 of file random.c.

References CLASS_OF, rb_cObject, rb_const_set(), rb_cRandom, rb_define_alloc_func(), rb_define_class(), rb_define_class_id(), rb_define_const(), rb_define_global_function, rb_define_method, rb_define_module_under(), rb_define_private_method, rb_define_singleton_method, rb_deprecate_constant, rb_extend_object(), rb_include_module(), rb_ractor_local_storage_ptr_newkey(), and rb_undef_alloc_func().

## ◆ NO_SANITIZE()

NO_SANITIZE | ( | "unsigned-integer-overflow" | ,
|  | extern st_index_t | rb_hash_startst_index_t h
| ) |  |

## ◆ random_mark()

void random_mark | ( | void * | ptr | ) |

Definition at line 236 of file random.c.

References ptr, and rb_gc_mark().

## ◆ rb_genrand_int32()

unsigned int rb_genrand_int32 | ( | void |  | ) |

Definition at line 189 of file random.c.

References rb_random_mt_t::mt.

## ◆ rb_genrand_real()

double rb_genrand_real | ( | void |  | ) |

Definition at line 196 of file random.c.

References rb_random_mt_t::mt.

## ◆ rb_genrand_ulong_limited()

unsigned long rb_genrand_ulong_limited | ( | unsigned long | limit | ) |

Definition at line 975 of file random.c.

References rb_random_mt_t::base.

## ◆ rb_hash_start()

st_index_t rb_hash_start | ( | st_index_t | h | ) |

Definition at line 1623 of file random.c.

References st_hash_start.

## ◆ rb_int_pair_to_real()

double rb_int_pair_to_real | ( | uint32_t | a,
|  | uint32_t | b,
|  | int | excl
| ) |  |

Definition at line 1036 of file random.c.

## ◆ rb_memhash()

st_index_t rb_memhash | ( | const void * | ptr,
|  | long | len
| ) |  |

Definition at line 1629 of file random.c.

References len, ptr, sip_hash13, and sip_uint64_t::u32.

Referenced by rb_big_hash(), rb_complex_hash(), rb_dbl_long_hash(), rb_rational_hash(), and rb_str_hash().

## ◆ rb_rand_bytes_int32()

void rb_rand_bytes_int32 | ( | rb_random_get_int32_func * | get_int32,
|  | rb_random_t * | rnd,
|  | void * | p,
|  | size_t | n
| ) |  |

Definition at line 1182 of file random.c.

References CHAR_BIT, ptr, and SIZEOF_INT32.

## ◆ rb_random_base_init()

void rb_random_base_init | ( | rb_random_t * | rnd | ) |

Definition at line 333 of file random.c.

References INT2FIX, and rb_random_struct::seed.

## ◆ rb_random_bytes()

VALUE rb_random_bytes | ( | VALUE | obj,
|  | long | n
| ) |  |

Definition at line 1205 of file random.c.

References NULL.

## ◆ rb_random_int32()

unsigned int rb_random_int32 | ( | VALUE | obj | ) |

Definition at line 1004 of file random.c.

## ◆ RB_RANDOM_INTERFACE_DECLARE()

RB_RANDOM_INTERFACE_DECLARE | ( | rand_mt |  | ) |

## ◆ rb_random_real()

double rb_random_real | ( | VALUE | obj | ) |

Definition at line 1047 of file random.c.

References NUM2DBL, rb_eRangeError, rb_funcallv, rb_raise(), and TRUE.

## ◆ rb_random_ulong_limited()

unsigned long rb_random_ulong_limited | ( | VALUE | obj,
|  | unsigned long | limit
| ) |  |

Definition at line 1107 of file random.c.

References NUM2ULONG, rb_eRangeError, rb_funcallv_public(), rb_num_negative_p(), rb_raise(), and rb_to_int().

## ◆ rb_reset_random_seed()

void rb_reset_random_seed | ( | void |  | ) |

Definition at line 1664 of file random.c.

References rb_random_mt_t::base, INT2FIX, rb_random_mt_t::mt, rb_random_struct::seed, and uninit_genrand.

## ◆ ruby_fill_random_bytes()

int ruby_fill_random_bytes | ( | void * | seed,
|  | size_t | size,
|  | int | need_secure
| ) |  |

Definition at line 575 of file random.c.

References fill_random_bytes_syscall, and fill_random_bytes_urandom.

## Variable Documentation

## ◆ key

hash_salt_t key

Definition at line 1608 of file random.c.

## ◆ rb_cRandom

VALUE rb_cRandom

Definition at line 226 of file random.c.

Referenced by InitVM_Random().

## ◆ rb_random_data_type

const rb_data_type_t rb_random_data_type

Initial value:
= {
"random",
{
random_mark,
random_free,
random_memsize,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
}
random_mark
#define random_mark
Definition: random.c:233
random_free
#define random_free
Definition: random.c:241
RUBY_TYPED_FREE_IMMEDIATELY
@ RUBY_TYPED_FREE_IMMEDIATELY
Definition: rtypeddata.h:62

Definition at line 249 of file random.c.

## ◆ u32

uint32_t u32[type_roomof(hash_salt_t, uint32_t)]

Definition at line 1609 of file random.c.