bw_rand

Pseudo-random number generators.

These are good enough for music DSP applications but should not be considered crypthographically secure.

All functions in this module are reentrant, RT-safe, thread-safe, and have no side effects.

The algorithms are based on the "simplified" Permuted Congruential Generator (PCG) by Chris Wellons, which is itself taken from

M. E. O'Neill, "PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation", September 2014, available at https://www.cs.hmc.edu/tr/hmc-cs-2014-0905.pdf.

API

Module type: Utility

bw_randu32()

static inline uint32_t bw_randu32(
	uint64_t * BW_RESTRICT state);

Returns a pseudo-random unsigned 32-bit integer in the range [0, UINT32_MAX].

state is a pointer to a 64-bit unsigned integer storing the state between calls and which gets updated by this function.

bw_randf()

static inline float bw_randf(
	uint64_t * BW_RESTRICT state);

Returns a pseudo-random unsigned 32-bit floating point number in the range [-1.f, 1.f].

state is a pointer to a 64-bit unsigned integer storing the state between calls and which gets updated by this function.

Changelog

  • Version 1.1.0:
    • Added support for BW_INCLUDE_WITH_QUOTES and BW_CXX_NO_EXTERN_C.
  • Version 1.0.1:
    • Now using BW_NULL.
  • Version 1.0.0:
    • Added one more assertion in bw_randf().
    • Removed usage of reserved identifiers.
  • Version 0.6.0:
    • Added debugging code.
    • Removed dependency on bw_config.
    • Fixed harmless warning in bw_randu32().
  • Version 0.2.0:
    • Refactored API.
  • Version 0.1.0:
    • First release.