This document provides detailed information about a custom random number generator implementation in C.
The API provides a set of functions for generating random numbers with various distributions and formats. It uses a state-based approach, allowing for multiple independent random number generators to be used simultaneously.
typedef struct {
int next_index;
int next_p_index;
int seed_array[SEED_ARRAY_SIZE];
} random_state_t;
A structure that holds the state of the random number generator.
next_index
: The next index in the seed array to be usednext_p_index
: The next p-index in the seed array to be usedseed_array
: An array of 56 integers used to generate random numbers
SEED_ARRAY_SIZE
: Set to 56, defines the size of the seed arrayMSEED
: Set to 161803398, used in the initialization process
void random_init(random_state_t* state, int seed);
Initializes a random number generator state with a given seed.
state
: Pointer to the random state structure to initializeseed
: An integer seed value
This function sets up the initial state of the random number generator. It uses the provided seed to generate a sequence of numbers that will be used for subsequent random number generation.
int random_internal_sample(random_state_t* state);
Generates the next random number in the sequence.
state
: Pointer to the current random state
An integer between 0 and INT_MAX
This is an internal function that generates the next random number in the sequence. It updates the state of the generator and returns a new random value.
int random_next(random_state_t* state, int min_value, int max_value);
Generates a random integer within a specified range.
state
: Pointer to the current random statemin_value
: The minimum value (inclusive)max_value
: The maximum value (inclusive)
A random integer between min_value and max_value, or -1 if min_value > max_value
This function generates a random integer that falls within the specified range [min_value, max_value].
int random_next_max(random_state_t* state, int max_value);
Generates a random integer between 0 and a specified maximum value.
state
: Pointer to the current random statemax_value
: The maximum value (exclusive)
A random integer between 0 (inclusive) and max_value (exclusive), or -1 if max_value < 0
This function generates a random integer that is less than the specified maximum value.
double random_next_double(random_state_t* state);
Generates a random double between 0.0 and 1.0.
state
: Pointer to the current random state
A random double between 0.0 (inclusive) and 1.0 (exclusive)
This function generates a random floating-point number in the range [0.0, 1.0).
void random_next_bytes(random_state_t* state, unsigned char* buffer, int size);
Fills a buffer with random bytes.
state
: Pointer to the current random statebuffer
: Pointer to the buffer to fill with random bytessize
: The number of bytes to generate
This function fills the provided buffer with random bytes. It generates 'size' number of random bytes and stores them in the buffer.
random_state_t state;
random_init(&state, 12345); // Initialize with seed 12345
int random_int = random_next(&state, 1, 100); // Get random int between 1 and 100
double random_float = random_next_double(&state); // Get random double between 0 and 1
unsigned char buffer[10];
random_next_bytes(&state, buffer, 10); // Fill buffer with 10 random bytes
- The random number generator uses a variation of the subtractive random number generator algorithm
- It's not cryptographically secure and should not be used for security purposes
- The period of the random number generator is approximately 2^55