Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1
80
81#ifndef BJ_RANDOM_H
82#define BJ_RANDOM_H
83
84#include <banjo/api.h>
85#include <banjo/math.h>
86
94#define BJ_RAND_MAX 0x7FFF
95
100BANJO_EXPORT void bj_srand(unsigned int seed);
101
112
117struct bj_pcg32 {
118 uint64_t state;
119 uint64_t inc;
120};
121
122struct bj_pcg32;
123
132 struct bj_pcg32* generator,
133 uint64_t seed,
134 uint64_t seq
135);
136
144 struct bj_pcg32* generator
145);
146
154 struct bj_pcg32* generator,
155 uint64_t z
156);
157
163
169
174
185static BJ_INLINE uint32_t bj_pcg32_generator(void* state) {
186 return bj_next_pcg32((struct bj_pcg32*)state);
187}
188
209static BJ_INLINE uint32_t bj_rand_generator(void* state) {
210 (void)state;
211 const uint32_t high = (uint32_t)bj_rand();
212 const uint32_t mid = (uint32_t)bj_rand();
213 const uint32_t low = (uint32_t)bj_rand();
214 return (high << 17) | (mid << 2) | (low >> 13);
215}
216
223typedef uint32_t (*bj_random_u32_fn)(void* state);
224
236 bj_random_u32_fn next,
237 void* state,
238 int32_t low,
239 int32_t high
240);
241
258 bj_random_u32_fn next,
259 void* state,
260 uint32_t low,
261 uint32_t high
262);
263
276 bj_random_u32_fn next,
277 void* state,
278 float low,
279 float high
280);
281
294 bj_random_u32_fn next,
295 void* state,
296 double low,
297 double high
298);
299
312 bj_random_u32_fn next,
313 void* state,
314 long double low,
315 long double high
316);
317
327#if defined(BJ_API_LONG_DOUBLE)
328 #define bj_random_uniform bj_random_uniform_ld
329#elif defined(BJ_API_FLOAT64)
330 #define bj_random_uniform bj_random_uniform_f64
331#else
332 #define bj_random_uniform bj_random_uniform_f32
333#endif
334
346 bj_random_u32_fn next,
347 void* state,
348 bj_real probability
349);
350
363 bj_random_u32_fn next,
364 void* state,
365 float mean,
366 float standard_deviation
367);
368
381 bj_random_u32_fn next,
382 void* state,
383 double mean,
384 double standard_deviation
385);
386
399 bj_random_u32_fn next,
400 void* state,
401 long double mean,
402 long double standard_deviation
403);
404
414#if defined(BJ_API_LONG_DOUBLE)
415 #define bj_random_normal bj_random_normal_ld
416#elif defined(BJ_API_FLOAT64)
417 #define bj_random_normal bj_random_normal_f64
418#else
419 #define bj_random_normal bj_random_normal_f32
420#endif
421
437 bj_random_u32_fn next,
438 void* state,
439 float min,
440 float max
441);
442
458 bj_random_u32_fn next,
459 void* state,
460 double min,
461 double max
462);
463
479 bj_random_u32_fn next,
480 void* state,
481 long double min,
482 long double max
483);
484
494#if defined(BJ_API_LONG_DOUBLE)
495 #define bj_random_normal_range bj_random_normal_range_ld
496#elif defined(BJ_API_FLOAT64)
497 #define bj_random_normal_range bj_random_normal_range_f64
498#else
499 #define bj_random_normal_range bj_random_normal_range_f32
500#endif
501
502// --- Deprecated names (renamed on the road to 2.0) --------------------------
503
506 int32_t low, int32_t high);
509 uint32_t low, uint32_t high);
512 float low, float high);
515 double low, double high);
518 long double low, long double high);
523 float mean, float standard_deviation);
526 double mean, double standard_deviation);
529 long double mean, long double standard_deviation);
532 float min, float max);
535 double min, double max);
538 long double min, long double max);
539
541#define bj_uniform_real_distribution bj_random_uniform
543#define bj_normal_real_distribution bj_random_normal
545#define bj_normal_real_minmax_distribution bj_random_normal_range
546
547#endif /* BJ_RANDOM_H */
General-purpose definitions for Banjo API.
#define BANJO_EXPORT
Definition api.h:179
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
Definition api.h:248
float bj_real
Selected real type for float configuration.
Definition math.h:80
uint64_t inc
Stream selector; odd recommended, 0 allowed.
Definition random.h:119
uint64_t state
Current internal state (updated each step).
Definition random.h:118
void bj_seed_pcg32(struct bj_pcg32 *generator, uint64_t seed, uint64_t seq)
Set the generator state from seed and sequence.
int bj_random_bernoulli(bj_random_u32_fn next, void *state, bj_real probability)
Bernoulli(probability).
long double bj_random_normal_ld(bj_random_u32_fn next, void *state, long double mean, long double standard_deviation)
Normal long double N(mean, standard_deviation^2).
long double bj_random_uniform_ld(bj_random_u32_fn next, void *state, long double low, long double high)
Uniform long double in [low, high).
uint32_t bj_pcg32_min(void)
Smallest possible value returned by the generator.
uint32_t bj_min_pcg32(void)
uint32_t bj_uniform_uint32_distribution(bj_random_u32_fn next, void *state, uint32_t low, uint32_t high)
long double bj_normal_long_double_minmax_distribution(bj_random_u32_fn next, void *state, long double min, long double max)
double bj_normal_double_distribution(bj_random_u32_fn next, void *state, double mean, double standard_deviation)
double bj_normal_double_minmax_distribution(bj_random_u32_fn next, void *state, double min, double max)
static uint32_t bj_pcg32_generator(void *state)
Adapter for distribution API (void* state).
Definition random.h:185
float bj_normal_float_minmax_distribution(bj_random_u32_fn next, void *state, float min, float max)
int32_t bj_uniform_int32_distribution(bj_random_u32_fn next, void *state, int32_t low, int32_t high)
long double bj_uniform_long_double_distribution(bj_random_u32_fn next, void *state, long double low, long double high)
int32_t bj_random_uniform_i32(bj_random_u32_fn next, void *state, int32_t low, int32_t high)
Uniform signed 32-bit integer in [low, high].
double bj_random_uniform_f64(bj_random_u32_fn next, void *state, double low, double high)
Uniform double in [low, high).
int bj_bernoulli_distribution(bj_random_u32_fn next, void *state, bj_real probability)
long double bj_random_normal_range_ld(bj_random_u32_fn next, void *state, long double min, long double max)
Normal long double from a min/max pair (μ ≈ (min+max)/2, σ ≈ (max-min)/6).
float bj_random_normal_range_f32(bj_random_u32_fn next, void *state, float min, float max)
Normal float from a min/max pair (μ ≈ (min+max)/2, σ ≈ (max-min)/6).
void bj_discard_pcg32(struct bj_pcg32 *generator, uint64_t z)
Advance the generator state by z steps.
float bj_normal_float_distribution(bj_random_u32_fn next, void *state, float mean, float standard_deviation)
static uint32_t bj_rand_generator(void *state)
Adapter exposing the global bj_rand engine to the distribution API.
Definition random.h:209
double bj_random_normal_range_f64(bj_random_u32_fn next, void *state, double min, double max)
Normal double from a min/max pair (μ ≈ (min+max)/2, σ ≈ (max-min)/6).
float bj_uniform_float_distribution(bj_random_u32_fn next, void *state, float low, float high)
uint32_t bj_random_uniform_u32(bj_random_u32_fn next, void *state, uint32_t low, uint32_t high)
Uniform unsigned 32-bit integer in [low, high].
uint32_t bj_max_pcg32(void)
float bj_random_normal_f32(bj_random_u32_fn next, void *state, float mean, float standard_deviation)
Normal float N(mean, standard_deviation^2).
long double bj_normal_long_double_distribution(bj_random_u32_fn next, void *state, long double mean, long double standard_deviation)
void bj_srand(unsigned int seed)
Seed the standard PRNG.
double bj_uniform_double_distribution(bj_random_u32_fn next, void *state, double low, double high)
uint32_t bj_pcg32_max(void)
Largest possible value returned by the generator.
double bj_random_normal_f64(bj_random_u32_fn next, void *state, double mean, double standard_deviation)
Normal double N(mean, standard_deviation^2).
int bj_rand(void)
Generate a pseudo-random integer in [0, BJ_RAND_MAX].
uint32_t bj_next_pcg32(struct bj_pcg32 *generator)
Advance the generator and return the next 32-bit value.
uint32_t(* bj_random_u32_fn)(void *state)
RNG callback type for generator-agnostic distributions.
Definition random.h:223
float bj_random_uniform_f32(bj_random_u32_fn next, void *state, float low, float high)
Uniform float in [low, high).
PCG32 generator state.
Definition random.h:117
C99 math shim with bj_real precision type and scalar utilities.