Ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e0ba6b95ab71a441357ed5484e33498)
random.h
Go to the documentation of this file.
1#ifndef RUBY_RANDOM_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RUBY_RANDOM_H 1
17#include "ruby/ruby.h"
18
20
21
31 VALUE seed;
32};
47typedef void rb_random_init_func(rb_random_t *rng, const uint32_t *buf, size_t len);
48
57typedef unsigned int rb_random_get_int32_func(rb_random_t *rng);
58
69typedef void rb_random_get_bytes_func(rb_random_t *rng, void *buf, size_t len);
70
80typedef double rb_random_get_real_func(rb_random_t *rng, int excl);
81
83typedef struct {
86
89
92
110
131
136#define RB_RANDOM_INTERFACE_DECLARE(prefix) \
137 static void prefix##_init(rb_random_t *, const uint32_t *, size_t); \
138 static unsigned int prefix##_get_int32(rb_random_t *); \
139 static void prefix##_get_bytes(rb_random_t *, void *, size_t)
140
145#define RB_RANDOM_INTERFACE_DECLARE_WITH_REAL(prefix) \
146 RB_RANDOM_INTERFACE_DECLARE(prefix); \
147 static double prefix##_get_real(rb_random_t *, int)
148
163#define RB_RANDOM_INTERFACE_DEFINE(prefix) \
164 prefix##_init, \
165 prefix##_get_int32, \
166 prefix##_get_bytes
167
172#define RB_RANDOM_INTERFACE_DEFINE_WITH_REAL(prefix) \
173 RB_RANDOM_INTERFACE_DEFINE(prefix), \
174 prefix##_get_real
175
176#if defined _WIN32 && !defined __CYGWIN__
178# define RB_RANDOM_PARENT 0
179#else
180
183
201# define RB_RANDOM_PARENT &rb_random_data_type
202#endif
203
210#define RB_RANDOM_DATA_INIT_PARENT(random_data) \
211 rbimpl_random_data_init_parent(&random_data)
212
220void rb_random_mark(void *ptr);
221
230
245double rb_int_pair_to_real(uint32_t a, uint32_t b, int excl);
246
259void rb_rand_bytes_int32(rb_random_get_int32_func *func, rb_random_t *prng, void *buff, size_t size);
260
266
268
270/* :TODO: can this function be __attribute__((returns_nonnull)) or not? */
277static inline const rb_random_interface_t *
278rb_rand_if(VALUE obj)
279{
281 const struct rb_data_type_struct *t = RTYPEDDATA_TYPE(obj);
282 const void *ret = t->data;
283 return RBIMPL_CAST((const rb_random_interface_t *)ret);
284}
285
296static inline void
297rbimpl_random_data_init_parent(rb_random_data_type_t *random_data)
298{
299#if defined _WIN32 && !defined __CYGWIN__
300 random_data->parent = &rb_random_data_type;
301#endif
302}
303
304#endif /* RUBY_RANDOM_H */
#define RBIMPL_ASSERT_OR_ASSUME(expr)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition: assert.h:229
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition: dllexport.h:47
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:106
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:97
const rb_data_type_t rb_random_data_type
The data that holds the backend type of rb_cRandom.
Definition: random.c:252
void rb_random_mark(void *ptr)
This is the implementation of rb_data_type_struct::dmark for rb_random_data_type.
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.
Definition: random.h:69
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.
Definition: random.h:47
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:1251
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:57
const rb_data_type_t rb_random_data_type_t
This is the type of rb_random_data_type.
Definition: random.h:182
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:1105
static const rb_random_interface_t * rb_rand_if(VALUE obj)
Queries the interface of the passed random object.
Definition: random.h:278
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.
Definition: random.h:80
void rb_random_base_init(rb_random_t *rnd)
Initialises an allocated rb_random_t instance.
Definition: random.c:329
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition: noalias.h:62
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition: nonnull.h:27
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition: pure.h:38
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition: rtypeddata.h:540
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition: rtypeddata.h:563
This is the struct that holds necessary info for a struct.
PRNG algorithmic interface, analogous to Ruby level classes.
Definition: random.h:83
rb_random_init_func * init
Initialiser function.
Definition: random.h:88
size_t default_seed_bits
Number of bits of seed numbers.
Definition: random.h:85
rb_random_get_int32_func * get_int32
Function to obtain a random integer.
Definition: random.h:91
rb_random_get_real_func * get_real
Function to obtain a random double.
Definition: random.h:129
rb_random_get_bytes_func * get_bytes
Function to obtain a series of random bytes.
Definition: random.h:109
Base components of the random interface.
Definition: random.h:29
VALUE seed
Seed, passed through e.g.
Definition: random.h:31