Ruby
3.4.0dev (2024-11-05 revision 348a53415339076afc4a02fcd09f3ae36e9c4c61)
|
#include "ruby/ruby.h"
Go to the source code of this file.
Data Structures | |
struct | rb_random_struct |
Base components of the random interface. More... | |
struct | rb_random_interface_t |
PRNG algorithmic interface, analogous to Ruby level classes. More... | |
Macros | |
#define | RUBY_RANDOM_INTERFACE_VERSION_MAJOR 1 |
#define | RUBY_RANDOM_INTERFACE_VERSION_MINOR 0 |
#define | RUBY_RANDOM_PASTE_VERSION_SUFFIX(x, y, z) x##_##y##_##z |
#define | RUBY_RANDOM_WITH_VERSION_SUFFIX(name, major, minor) RUBY_RANDOM_PASTE_VERSION_SUFFIX(name, major, minor) |
#define | rb_random_data_type |
#define | RUBY_RANDOM_INTERFACE_VERSION_INITIALIZER {RUBY_RANDOM_INTERFACE_VERSION_MAJOR, RUBY_RANDOM_INTERFACE_VERSION_MINOR} |
#define | RUBY_RANDOM_INTERFACE_VERSION_MAJOR_MAX 0xff |
#define | RUBY_RANDOM_INTERFACE_VERSION_MINOR_MAX 0xff |
#define | RB_RANDOM_INTERFACE_DECLARE(prefix) |
This utility macro defines 4 functions named prefix_init, prefix_init_int32, prefix_get_int32, prefix_get_bytes. More... | |
#define | RB_RANDOM_INTERFACE_DECLARE_WITH_REAL(prefix) |
Identical to RB_RANDOM_INTERFACE_DECLARE except it also declares prefix_get_real. More... | |
#define | RB_RANDOM_INTERFACE_DEFINE(prefix) |
This utility macro expands to the names declared using RB_RANDOM_INTERFACE_DECLARE. More... | |
#define | RB_RANDOM_INTERFACE_DEFINE_WITH_REAL(prefix) |
Identical to RB_RANDOM_INTERFACE_DEFINE except it also defines prefix_get_real. More... | |
#define | RB_RANDOM_DEFINE_INIT_INT32_FUNC(prefix) |
#define | RB_RANDOM_PARENT &rb_random_data_type |
This utility macro can be used when you define your own PRNG type: More... | |
#define | RB_RANDOM_DATA_INIT_PARENT(random_data) rbimpl_random_data_init_parent(&random_data) |
This macro is expected to be called exactly once at the beginning of a program, possibly from inside of your Init_Foo() function. More... | |
Typedefs | |
typedef struct rb_random_struct | rb_random_t |
typedef void | rb_random_init_func(rb_random_t *rng, const uint32_t *buf, size_t len) |
This is the type of functions called when your random object is initialised. More... | |
typedef void | rb_random_init_int32_func(rb_random_t *rng, uint32_t data) |
This is the type of functions called when your random object is initialised. More... | |
typedef unsigned int | rb_random_get_int32_func(rb_random_t *rng) |
This is the type of functions called from your object's #rand method. More... | |
typedef void | rb_random_get_bytes_func(rb_random_t *rng, void *buf, size_t len) |
This is the type of functions called from your object's #bytes method. More... | |
typedef double | rb_random_get_real_func(rb_random_t *rng, int excl) |
This is the type of functions called from your object's #rand method. More... | |
typedef const rb_data_type_t | rb_random_data_type_t |
This is the type of rb_random_data_type. More... | |
Functions | |
void | rb_random_mark (void *ptr) |
This is the implementation of rb_data_type_struct::dmark for rb_random_data_type. More... | |
void | rb_random_base_init (rb_random_t *rnd) |
Initialises an allocated rb_random_t instance. More... | |
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. More... | |
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 random bytes. More... | |
static const rb_random_interface_t * | rb_rand_if (VALUE obj) |
Queries the interface of the passed random object. More... | |
Variables | |
const rb_data_type_t | rb_random_data_type |
The data that holds the backend type of rb_cRandom. More... | |
This is a set of APIs to roll your own subclass of rb_cRandom. An illustrative example of such PRNG can be found at ext/-test-/random/loop.c
.
Definition in file random.h.
#define RB_RANDOM_DATA_INIT_PARENT | ( | random_data | ) | rbimpl_random_data_init_parent(&random_data) |
This macro is expected to be called exactly once at the beginning of a program, possibly from inside of your Init_Foo()
function.
Depending on platforms RB_RANDOM_PARENT can require a fixup. This routine does that when necessary.
#define rb_random_data_type |
#define RB_RANDOM_DEFINE_INIT_INT32_FUNC | ( | prefix | ) |
#define RB_RANDOM_INTERFACE_DECLARE | ( | prefix | ) |
This utility macro defines 4 functions named prefix_init, prefix_init_int32, prefix_get_int32, prefix_get_bytes.
#define RB_RANDOM_INTERFACE_DECLARE_WITH_REAL | ( | prefix | ) |
Identical to RB_RANDOM_INTERFACE_DECLARE except it also declares prefix_get_real.
#define RB_RANDOM_INTERFACE_DEFINE | ( | prefix | ) |
This utility macro expands to the names declared using RB_RANDOM_INTERFACE_DECLARE.
Expected to be used inside of a rb_random_interface_t initialiser:
#define RB_RANDOM_INTERFACE_DEFINE_WITH_REAL | ( | prefix | ) |
Identical to RB_RANDOM_INTERFACE_DEFINE except it also defines prefix_get_real.
#define RB_RANDOM_PARENT &rb_random_data_type |
This utility macro can be used when you define your own PRNG type:
typedef const rb_data_type_t rb_random_data_type_t |
This is the type of rb_random_data_type.
typedef void rb_random_get_bytes_func(rb_random_t *rng, void *buf, size_t len) |
This is the type of functions called from your object's #bytes
method.
[out] | rng | Your random struct to extract an integer from. |
[out] | buf | Return buffer of at least len bytes length. |
[in] | len | Number of bytes of buf . |
rng
is consumed somehow. buf
is filled with random bytes. typedef unsigned int rb_random_get_int32_func(rb_random_t *rng) |
typedef double rb_random_get_real_func(rb_random_t *rng, int excl) |
This is the type of functions called from your object's #rand
method.
[out] | rng | Your random struct to extract an integer from. |
[in] | excl | Pass nonzero value here to indicate you don't want 1.0. |
rng
is consumed somehow. typedef void rb_random_init_func(rb_random_t *rng, const uint32_t *buf, size_t len) |
This is the type of functions called when your random object is initialised.
Passed buffer is the seed object basically. But in Ruby a number can be really big. This type of functions accept such big integers as a series of machine words.
[out] | rng | Your random struct to fill in. |
[in] | buf | Seed, maybe converted from a bignum. |
[in] | len | Number of words of buf . |
rng
is initialised using the passed seeds. typedef void rb_random_init_int32_func(rb_random_t *rng, uint32_t data) |
typedef struct rb_random_struct rb_random_t |
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.
[in] | a | Most significant 32 bits of the result. |
[in] | b | Least significant 32 bits of the result. |
[in] | excl | Whether the result should exclude 1.0 or not. |
[0, 1)
or [0, 1]
. 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 random bytes.
[in] | func | Generator function. |
[out] | prng | Passed as-is to func . |
[out] | buff | Return buffer. |
[in] | size | Number of words of buff . |
buff
is filled with random bytes. prng
is updated by func
.
|
inlinestatic |
Queries the interface of the passed random object.
[in] | obj | An instance (of a subclass) of rb_cRandom. |
void rb_random_base_init | ( | rb_random_t * | rnd | ) |
Initialises an allocated rb_random_t instance.
Call it from your own initialiser appropriately.
[out] | rnd | Your PRNG's base part. |
rnd
is filled with an initial state. void rb_random_mark | ( | void * | ptr | ) |
This is the implementation of rb_data_type_struct::dmark for rb_random_data_type.
In case your PRNG does not involve Ruby objects at all (which is quite likely), you can simply reuse it.
[out] | ptr | Target to mark, which is a rb_random_t this case. |
|
extern |
The data that holds the backend type of rb_cRandom.
Used as your PRNG's rb_data_type_struct::parent.