CRandom¶
A random number generation implementation in C similar to the one found in standard C++
- Distribution functions
- Distribution functions
- Xoshiro256**
- Xoshiro256** random number generator
Types¶
Name | Description |
---|---|
random_device_t | Random Device type. |
random_engine_t | Random engine type. |
RandomEngineSpec | Random engine specification structure. |
Xoshiro256ssSpec | Specification for the Xoshiro256** random engine. |
Functions¶
Name | Description |
---|---|
random_device_ctor_token | Construct a random device from a token. |
random_device_ctor | Construct a random device. |
random_device_read | Read random data from the random device. |
random_device_read_s | Read random data from the random device in a safe manner. |
random_device_dtor | Free memory used by the random device. |
random_engine_ctor | Construct a random engine using a specific specification. Primarily designed to facilitate the creation of custom random engines. |
random_engine_next | Get the next random number from the random engine. |
random_engine_dtor | Free memory used by the random engine. |
random_engine_get_spec | Get specification of the random engine. |
random_engine_data | Get data of the random engine. |
Function Details¶
random_device_ctor_token¶
random_device_t *random_device_ctor_token(const char *token)
Construct a random device from a token.
-
token
- A token to construct the random device from.
Note
Possible tokens on Unix-like systems: /dev/urandom
and /dev/random
.
default is /dev/urandom
.
Note
On Windows, the token is ignored and the default random device is used.
- Return
- A pointer to the random device instance.
random_device_ctor¶
random_device_t *random_device_ctor(void)
Construct a random device.
- Return
- A pointer to the random device instance.
random_device_read¶
size_t random_device_read(random_device_t *instance, void *buffer, size_t count)
Read random data from the random device.
-
instance
- A pointer to the random device instance.
-
buffer
- A pointer to the buffer to write the random data to.
-
count
- The number of bytes to read. Must be less than or equal to the size of the buffer.
- Return
- The number of bytes read.
random_device_read_s¶
size_t random_device_read_s(random_device_t *instance, void *buffer, size_t buffer_size, size_t count)
Read random data from the random device in a safe manner.
-
instance
- A pointer to the random device instance.
-
buffer
- A pointer to the buffer to write the random data to.
-
buffer_size
- The size of the buffer.
-
count
- The number of bytes to read.
- Return
- The number of bytes read.
random_device_dtor¶
void random_device_dtor(random_device_t *instance)
Free memory used by the random device.
Note
This function should release any resources allocated for the random device.
-
instance
- A pointer to the random device instance.
random_engine_ctor¶
random_engine_t * random_engine_ctor(random_engine_spec_t spec, void *data)
Construct a random engine using a specific specification. Primarily designed to facilitate the creation of custom random engines.
-
spec
- A pointer to the random engine specification.
-
data
- A pointer to the data to be used by the random engine.
Warning
Spec pointer must point to a static instance.
- Return
- A pointer to the random engine instance.
random_engine_next¶
uint64_t random_engine_next(random_engine_t *instance)
Get the next random number from the random engine.
-
instance
- A pointer to the random engine instance.
- Return
- The next random number in the range [0, UINT64_MAX].
random_engine_dtor¶
void random_engine_dtor(random_engine_t *engine)
Free memory used by the random engine.
Note
This function will auto-release data by calling the data destructor function in the random engine specification.
-
engine
- A pointer to the random engine instance.
random_engine_get_spec¶
random_engine_spec_t random_engine_get_spec(random_engine_t *engine)
Get specification of the random engine.
Note
This function is useful when you implement a custom random engine.
-
engine
- A pointer to the random engine instance.
- Return
- A pointer to the random engine specification.
random_engine_data¶
void *random_engine_data(random_engine_t *engine)
Get data of the random engine.
Note
This function is useful when you implement a custom random engine.
-
engine
- A pointer to the random engine instance.
- Return
- A pointer to the random engine data.