Mstdlib-1.24.0
Pseudo Random Number Generator (PRNG)

Macros

#define M_RAND_MAX   M_UINT64_MAX
 

Typedefs

typedef struct M_rand M_rand_t
 

Functions

M_rand_tM_rand_create (M_uint64 seed)
 
void M_rand_destroy (M_rand_t *state)
 
M_uint64 M_rand (M_rand_t *state)
 
M_uint64 M_rand_range (M_rand_t *state, M_uint64 min, M_uint64 max)
 
M_uint64 M_rand_max (M_rand_t *state, M_uint64 max)
 
M_bool M_rand_str (M_rand_t *state, const char *charset, char *out, size_t len)
 
M_rand_tM_rand_duplicate (const M_rand_t *state)
 
void M_rand_jump (M_rand_t *state)
 

Detailed Description

This is NOT a cryptographically secure RNG. This should NEVER be used for cryptographic operations.

Macro Definition Documentation

◆ M_RAND_MAX

#define M_RAND_MAX   M_UINT64_MAX

Typedef Documentation

◆ M_rand_t

typedef struct M_rand M_rand_t

Function Documentation

◆ M_rand_create()

M_rand_t * M_rand_create ( M_uint64  seed)

Create a random state for use with random number generation.

Parameters
[in]seedThe seed state. Using the same seed will allow the sequence to be repeated. If 0 the seed will be a combination of system time, local and heap memory addresses. The data is meant to make choosing a random seed harder but still is not cryptographically secure. This not being a cryptographically secure random number generator we are using random data that is not cryptographically secure for speed.
Returns
Random state.

◆ M_rand_destroy()

void M_rand_destroy ( M_rand_t state)

Destroy a random state.

Parameters
[in,out]stateThe state.

◆ M_rand()

M_uint64 M_rand ( M_rand_t state)

Generate a random number.

Generates a random number from 0 to M_RAND_MAX.

Parameters
[in,out]stateThe state. Optional, can be NULL, but will incur the overhead of M_rand_create(0); M_rand_destroy(); per iteration if not provided. It is possible, in a tight loop on a very fast system for example, the same number could be generated multiple times if the state is NULL.
Returns
A random number.

◆ M_rand_range()

M_uint64 M_rand_range ( M_rand_t state,
M_uint64  min,
M_uint64  max 
)

Generate a random number within a given range.

Range is [min, max). Meaning from min to max-1.

Parameters
[in,out]stateThe state. Optional, can be NULL, but will incur the overhead of M_rand_create(0); M_rand_destroy(); per iteration if not provided. It is possible, in a tight loop on a very fast system for example, the same number could be generated multiple times if the state is NULL.
[in]minThe min.
[in]maxThe max.

◆ M_rand_max()

M_uint64 M_rand_max ( M_rand_t state,
M_uint64  max 
)

Generate a random number with a given maximum.

Range is [0, max). Meaning from 0 to max-1.

Parameters
[in,out]stateThe state. Optional, can be NULL, but will incur the overhead of M_rand_create(0); M_rand_destroy(); per iteration if not provided. It is possible, in a tight loop on a very fast system for example, the same number could be generated multiple times if the state is NULL.
[in]maxThe max.

◆ M_rand_str()

M_bool M_rand_str ( M_rand_t state,
const char *  charset,
char *  out,
size_t  len 
)

Generate a random string based on the provided character set.

Parameters
[in,out]stateThe state. Optional, can be NULL, but will incur the overhead of M_rand_create(0); M_rand_destroy(); per iteration if not provided. It is possible, in a tight loop on a very fast system for example, the same string could be generated multiple times if the state is NULL.
[in]charsetCharacter set to use to generate the random string.
[out]outBuffer to use to hold the resulting random string. Must be len+1 bytes in length or greater to handle NULL terminator.
[in]lenNumber of characters to generate. Actual number of bytes written will be 1 larger for the NULL terminator.
Returns
M_FALSE on usage error. M_TRUE on success.

◆ M_rand_duplicate()

M_rand_t * M_rand_duplicate ( const M_rand_t state)

Duplicate the state of a random number generator.

If state is NULL this is equivalent to M_rand_create(0).

Parameters
[in,out]stateThe state.
Returns
Random state.
See also
M_rand_jump

◆ M_rand_jump()

void M_rand_jump ( M_rand_t state)

Advance the random number generator to generate non-overlapping sequences.

Parameters
[in,out]stateThe state.
See also
M_rand_duplicate