Banjo API 0.0.1
C99 game development API
Loading...
Searching...
No Matches
Random

Data Structures

struct  bj_pcg32_t

Macros

#define BJ_RAND_MAX   0x7FFF
#define bj_uniform_real_distribution   bj_uniform_float_distribution
#define bj_normal_real_distribution   bj_normal_float_distribution

Typedefs

typedef struct bj_pcg32_t bj_pcg32
typedef uint32_t(* bj_random_u32_fn_t) (void *state)

Functions

void bj_srand (unsigned int seed)
int bj_rand (void)
void bj_seed_pcg32 (bj_pcg32 *generator, uint64_t seed, uint64_t seq)
uint32_t bj_next_pcg32 (bj_pcg32 *generator)
void bj_discard_pcg32 (bj_pcg32 *generator, uint64_t z)
uint32_t bj_min_pcg32 (void)
uint32_t bj_max_pcg32 (void)
static uint32_t bj_pcg32_generator (void *state)
int32_t bj_uniform_int32_distribution (bj_random_u32_fn_t next, void *state, int32_t low, int32_t high)
float bj_uniform_float_distribution (bj_random_u32_fn_t next, void *state, float low, float high)
double bj_uniform_double_distribution (bj_random_u32_fn_t next, void *state, double low, double high)
long double bj_uniform_long_double_distribution (bj_random_u32_fn_t next, void *state, long double low, long double high)
int bj_bernoulli_distribution (bj_random_u32_fn_t next, void *state, bj_real probability)
float bj_normal_float_distribution (bj_random_u32_fn_t next, void *state, float mean, float standard_deviation)
double bj_normal_double_distribution (bj_random_u32_fn_t next, void *state, double mean, double standard_deviation)
long double bj_normal_long_double_distribution (bj_random_u32_fn_t next, void *state, long double mean, long double standard_deviation)

Detailed Description

Random number generation utilities.


Data Structure Documentation

◆ bj_pcg32_t

struct bj_pcg32_t

PCG32 generator state.

Data Fields
uint64_t inc Stream selector; odd recommended, 0 allowed.
uint64_t state Current internal state (updated each step).

Macro Definition Documentation

◆ bj_normal_real_distribution

#define bj_normal_real_distribution   bj_normal_float_distribution

Alias to the real-typed normal distribution for the active precision.

Maps to:

  • bj_normal_long_double_distribution if BJ_API_LONG_DOUBLE
  • bj_normal_double_distribution if BJ_API_FLOAT64
  • bj_normal_float_distribution otherwise

◆ BJ_RAND_MAX

#define BJ_RAND_MAX   0x7FFF

Maximum value returned by bj_rand().

Matches the stdlib idea of RAND_MAX. Values from bj_rand() are distributed in [0, BJ_RAND_MAX].

◆ bj_uniform_real_distribution

#define bj_uniform_real_distribution   bj_uniform_float_distribution

Alias to the real-typed uniform distribution for the active precision.

Maps to:

  • bj_uniform_long_double_distribution if BJ_API_LONG_DOUBLE
  • bj_uniform_double_distribution if BJ_API_FLOAT64
  • bj_uniform_float_distribution otherwise

Typedef Documentation

◆ bj_pcg32

typedef struct bj_pcg32_t bj_pcg32

Alias for bj_pcg32_t.

◆ bj_random_u32_fn_t

typedef uint32_t(* bj_random_u32_fn_t) (void *state)

RNG callback type for generator-agnostic distributions.

Parameters
stateOpaque engine state pointer.
Returns
Next 32-bit pseudo-random value.

Function Documentation

◆ bj_bernoulli_distribution()

int bj_bernoulli_distribution ( bj_random_u32_fn_t next,
void * state,
bj_real probability )

Bernoulli(probability).

Parameters
nextRNG callback (e.g., bj_pcg32_generator).
stateOpaque engine state for next.
probabilityProbability in [0,1].
Returns
1 with the given probability, else 0.

◆ bj_discard_pcg32()

void bj_discard_pcg32 ( bj_pcg32 * generator,
uint64_t z )

Advance the generator state by z steps.

Parameters
generatorGenerator pointer (must not be NULL).
zNumber of steps to skip ahead.

◆ bj_max_pcg32()

uint32_t bj_max_pcg32 ( void )

Largest possible value returned by the generator.

Returns
Always 0xFFFFFFFF.

◆ bj_min_pcg32()

uint32_t bj_min_pcg32 ( void )

Smallest possible value returned by the generator.

Returns
Always 0.

◆ bj_next_pcg32()

uint32_t bj_next_pcg32 ( bj_pcg32 * generator)

Advance the generator and return the next 32-bit value.

Parameters
generatorGenerator pointer (must not be NULL).
Returns
Next 32-bit pseudo-random value.

◆ bj_normal_double_distribution()

double bj_normal_double_distribution ( bj_random_u32_fn_t next,
void * state,
double mean,
double standard_deviation )

Normal double N(mean, standard_deviation^2).

Parameters
nextRNG callback (e.g., bj_pcg32_generator).
stateOpaque engine state for next.
meanMean.
standard_deviationStandard deviation (>= 0).
Returns
double sample.

◆ bj_normal_float_distribution()

float bj_normal_float_distribution ( bj_random_u32_fn_t next,
void * state,
float mean,
float standard_deviation )

Normal float N(mean, standard_deviation^2).

Parameters
nextRNG callback (e.g., bj_pcg32_generator).
stateOpaque engine state for next.
meanMean.
standard_deviationStandard deviation (>= 0).
Returns
float sample.

◆ bj_normal_long_double_distribution()

long double bj_normal_long_double_distribution ( bj_random_u32_fn_t next,
void * state,
long double mean,
long double standard_deviation )

Normal long double N(mean, standard_deviation^2).

Parameters
nextRNG callback (e.g., bj_pcg32_generator).
stateOpaque engine state for next.
meanMean.
standard_deviationStandard deviation (>= 0).
Returns
long double sample.

◆ bj_pcg32_generator()

uint32_t bj_pcg32_generator ( void * state)
inlinestatic

Adapter for distribution API (void* state).

Allows bj_pcg32 to be used with callbacks of type uint32_t (*)(void* state).

Parameters
statePointer to bj_pcg32.
Returns
Next 32-bit pseudo-random value.

◆ bj_rand()

int bj_rand ( void )

Generate a pseudo-random integer in [0, BJ_RAND_MAX].

Linear congruential generator (LCG) using: X_{n+1} = (1103515245 * X_n + 12345) mod 2^31

Returns the high-order bits truncated to BJ_RAND_MAX range.

Returns
Integer in [0, BJ_RAND_MAX].

◆ bj_seed_pcg32()

void bj_seed_pcg32 ( bj_pcg32 * generator,
uint64_t seed,
uint64_t seq )

Set the generator state from seed and sequence.

Parameters
generatorGenerator pointer (must not be NULL).
seedInitial seed value.
seqStream selector (LSB forced to 1 internally).

◆ bj_srand()

void bj_srand ( unsigned int seed)

Seed the standard PRNG.

Parameters
seedSeed value. Same seed reproduces the same sequence.

◆ bj_uniform_double_distribution()

double bj_uniform_double_distribution ( bj_random_u32_fn_t next,
void * state,
double low,
double high )

Uniform double in [low, high).

Parameters
nextRNG callback (e.g., bj_pcg32_generator).
stateOpaque engine state for next.
lowInclusive lower bound.
highExclusive upper bound.
Returns
double in [low, high).

◆ bj_uniform_float_distribution()

float bj_uniform_float_distribution ( bj_random_u32_fn_t next,
void * state,
float low,
float high )

Uniform float in [low, high).

Parameters
nextRNG callback (e.g., bj_pcg32_generator).
stateOpaque engine state for next.
lowInclusive lower bound.
highExclusive upper bound.
Returns
float in [low, high).

◆ bj_uniform_int32_distribution()

int32_t bj_uniform_int32_distribution ( bj_random_u32_fn_t next,
void * state,
int32_t low,
int32_t high )

Uniform 32-bit integer in [low, high].

Parameters
nextRNG callback (e.g., bj_pcg32_generator).
stateOpaque engine state for next.
lowInclusive lower bound.
highInclusive upper bound.
Returns
int32 in [low, high].

◆ bj_uniform_long_double_distribution()

long double bj_uniform_long_double_distribution ( bj_random_u32_fn_t next,
void * state,
long double low,
long double high )

Uniform long double in [low, high).

Parameters
nextRNG callback (e.g., bj_pcg32_generator).
stateOpaque engine state for next.
lowInclusive lower bound.
highExclusive upper bound.
Returns
long double in [low, high).