Ruby: rb_random_interface_t Struct Reference

Ruby 3.3.4p94 (2024-07-09 revision be1089c8ec5ba40e09b1553e36b3174bf4014d9d)

Data Fields
rb_random_interface_t Struct Reference

PRNG algorithmic interface, analogous to Ruby level classes. More...

`#include <random.h>`

## Data Fields
size_t | default_seed_bits
| Number of bits of seed numbers.
struct { |
uint8_t major |
uint8_t minor |
} | version |
| Major/minor versions of this interface.
uint16_t | flags
| Reserved flags.
rb_random_init_func * | init
| Function to initialize from uint32_t array.
rb_random_init_int32_func * | init_int32
| Function to initialize from single uint32_t.
rb_random_get_int32_func * | get_int32
| Function to obtain a random integer.
rb_random_get_bytes_func * | get_bytes
| Function to obtain a series of random bytes.
rb_random_get_real_func * | get_real
| Function to obtain a random double.

## Detailed Description

PRNG algorithmic interface, analogous to Ruby level classes.

Definition at line 114 of file random.h.

## Field Documentation

## ◆ default_seed_bits

size_t rb_random_interface_t::default_seed_bits

Number of bits of seed numbers.

Definition at line 116 of file random.h.

## ◆ flags

uint16_t rb_random_interface_t::flags

Reserved flags.

Definition at line 128 of file random.h.

## ◆ get_bytes

rb_random_get_bytes_func* rb_random_interface_t::get_bytes

Function to obtain a series of random bytes.

If your PRNG have a native method to yield arbitrary number of bytes use that to implement this. But in case you lack such things, you can do so by using rb_rand_bytes_int32()

extern rb_random_get_int32_func your_get_int32_func;
void
your_get_byes_func(rb_random_t *rng, void *buf, size_t len)
{
rb_rand_bytes_int32(your_get_int32_func, rng, buf, len);
}
len
int len
Length of the buffer.
Definition io.h:8
rb_rand_bytes_int32
void rb_rand_bytes_int32(rb_random_get_int32_func *func, rb_random_t *prng, void *buff, size_t size)
Repeatedly calls the passed function over and over again until the passed buffer is filled with rando...
Definition random.c:1278
rb_random_get_int32_func
unsigned int rb_random_get_int32_func(rb_random_t *rng)
This is the type of functions called from your object's #rand method.
Definition random.h:88
rb_random_struct
Base components of the random interface.
Definition random.h:49

Definition at line 155 of file random.h.

## ◆ get_int32

rb_random_get_int32_func* rb_random_interface_t::get_int32

Function to obtain a random integer.

Definition at line 137 of file random.h.

## ◆ get_real

rb_random_get_real_func* rb_random_interface_t::get_real

Function to obtain a random double.

If your PRNG have a native method to yield a floating point random number use that to implement this. But in case you lack such things, you can do so by using rb_int_pair_to_real().

extern rb_random_get_int32_func your_get_int32_func;
void
your_get_real_func(rb_random_t *rng, int excl)
{
auto a = your_get_int32_func(rng);
auto b = your_get_int32_func(rng);
return rb_int_pair_to_real(a, b, excl);
}
rb_int_pair_to_real
double rb_int_pair_to_real(uint32_t a, uint32_t b, int excl)
Generates a 64 bit floating point number by concatenating two 32bit unsigned integers.
Definition random.c:1132

Definition at line 175 of file random.h.

## ◆ init

rb_random_init_func* rb_random_interface_t::init

Function to initialize from uint32_t array.

Definition at line 131 of file random.h.

## ◆ init_int32

rb_random_init_int32_func* rb_random_interface_t::init_int32

Function to initialize from single uint32_t.

Definition at line 134 of file random.h.

## ◆ major

uint8_t rb_random_interface_t::major

Definition at line 122 of file random.h.

## ◆ minor

uint8_t rb_random_interface_t::minor

Definition at line 122 of file random.h.

## ◆ [struct]

struct { ... } rb_random_interface_t::version

Major/minor versions of this interface.

The documentation for this struct was generated from the following file:

- include/ruby/random.h