libGimbal 0.2.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_random.h
Go to the documentation of this file.
1/*! \file
2 * \brief Random number generators and utilities
3 * \ingroup random
4 *
5 * This file contains the API and types surrounding
6 * random number generation. A series of utilities are
7 * provided for generating different types of random
8 * output (integer, float, bool, string, etc) as well
9 * as a variety of random number distributions.
10 *
11 * \note
12 * gblRandLehmer() and the various statistical disbributions
13 * are based on:
14 * <i>"Random Number Generators: Good Ones Are Hard To Find"</i>
15 * Steve Park and Keith Miller
16 * Communications of the ACM, October 1988
17 *
18 * \todo
19 * - Changing channels to swap between seeds
20 *
21 * \author 1998 Steve Park
22 * \author 1998 Keith Miller
23 * \author 2023 Falco Girgis
24 */
25#ifndef GIMBAL_RANDOM_H
26#define GIMBAL_RANDOM_H
27
28#include "../core/gimbal_decls.h"
29
30#define GBL_SEED_COUNT 2 //!< Number of different seeds maintained
31
32#ifndef GBL_RAND_GENERATOR_DEFAULT
33//! Default random number generator backing gblRand()
34# define GBL_RAND_GENERATOR_DEFAULT gblRandLehmer
35#endif
36
38
39//! Function prototype for a custom random generator to be set with gblSetRand()
40typedef int (*GblRandomGeneratorFn)(void);
41
42/*! \defgroup random Random
43 * \ingroup algorithms
44 * \brief Random number generators and utilities
45*/
46
47/*! \name Basic API
48 * \brief Basic API for driving random number generation
49 * @{
50 */
51//! Returns the seed associated with the given \p index
52GBL_EXPORT uint64_t gblSeed (uint8_t index) GBL_NOEXCEPT;
53//! Sets the seed associated with the given \p index to \p seed
54GBL_EXPORT void gblSeedRand (uint8_t index, uint64_t seed) GBL_NOEXCEPT;
55//! Returns a random number between 0 and RAND_MAX using the current random number generator
57//! Sets the current random number generator to \p pFnGen, which drives gblRand()
59//! @}
60
61/*! \name Generators
62 * \brief Builtin random number generators
63 * @{
64 */
65//! The builtin rand() generator from the C standard library
67//! Lehmer linear congruential random number generator
69//! @}
70
71/*! \name Utilities
72 * \brief Utility functions for random data generation
73 * @{
74 */
75//! Generates a random boolean value using the current generator
77//! Generates a random single-precision float from FLT_MIN to FLT_MAX.
79//! Fills in the given buffer with random bytes using the current generator
80GBL_EXPORT void gblRandBuffer (void* pData, size_t size) GBL_NOEXCEPT;
81//! Fills in buffer with a sized random word (optionally generated using a list of characters)
82GBL_EXPORT int gblRandString (char* pBuffer,
83 int minSize,
84 int maxSize,
85 const char* pCharList/*=NULL*/) GBL_NOEXCEPT;
86//!@}
87
88/*! \name Discrete Distributions
89 * \brief Random number generation for different discrete distributions
90 * @{
91 */
92//! Generates a random integer using the current random number generator over a Bernoulli distribution
94//! Generates a random integer using the current random number generator over a Binomial distribution
96//! Generates a random integer using the current random number generator over the given range [a, b]
98//! Generates a random integer using the current random number generator over a Geometric distribution
100//! Generates a random integer using the current random number generator over a Pascal distribution
102//! Generates a random integer using the current random number generator over a Poisson distribution
104//! @}
105
106/*! \name Continuous Distributions
107 * \brief Random number generation for different continuous distributions
108 * @{
109 */
110//! Generates a random float using the current random number generator over the given range [a, b]
111GBL_EXPORT float gblRandUniform (float a, float b) GBL_NOEXCEPT;
112//! Generates a random float using the current random number generator over an Exponential distribution
114//! Generates a random float using the current random number generator over an Erlang distribution
115GBL_EXPORT float gblRandErlang (int n, float b) GBL_NOEXCEPT;
116//! Generates a random integer using the current random number generator over a Normal distribution
117GBL_EXPORT float gblRandNormal (float m, float s) GBL_NOEXCEPT;
118//! Generates a random integer using the current random number generator over a Log Normal distribution
119GBL_EXPORT float gblRandLogNormal (float a, float b) GBL_NOEXCEPT;
120//! Generates a random integer using the current random number generator over a Chisquare distribution
122//! Generates a random integer using the current random number generator over a Student distribution
124//! @}
125
127
128//! \cond
129#define gblRandString(...)
130 gblRandStringDefault_(__VA_ARGS__)
131#define gblRandStringDefault_(...)
132 gblRandStringDefault__(__VA_ARGS__, GBL_NULL)
133#define gblRandStringDefault__(buffer, min, max, chars, ...)
134 (gblRandString)(buffer, min, max, chars)
135//! \endcond
136
137#endif // GIMBAL_RANDOM_H
#define GBL_NULL
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_EXPORT
int gblRandPascal(int n, float p)
Generates a random integer using the current random number generator over a Pascal distribution.
float gblRandChisquare(int n)
Generates a random integer using the current random number generator over a Chisquare distribution.
float gblRandErlang(int n, float b)
Generates a random float using the current random number generator over an Erlang distribution.
int gblRandPoisson(float m)
Generates a random integer using the current random number generator over a Poisson distribution.
float gblRandLogNormal(float a, float b)
Generates a random integer using the current random number generator over a Log Normal distribution.
int gblRandLehmer(void)
Lehmer linear congruential random number generator.
int gblRandString(char *pBuffer, int minSize, int maxSize, const char *pCharList)
Fills in buffer with a sized random word (optionally generated using a list of characters)
void gblRandBuffer(void *pData, size_t size)
Fills in the given buffer with random bytes using the current generator.
int gblRandEquilikely(int a, int b)
Generates a random integer using the current random number generator over the given range [a,...
uint64_t gblSeed(uint8_t index)
Returns the seed associated with the given index.
float gblRandExponential(float m)
Generates a random float using the current random number generator over an Exponential distribution.
void gblSetRand(GblRandomGeneratorFn pFnGen)
Sets the current random number generator to pFnGen, which drives gblRand()
float gblRandf(void)
Generates a random single-precision float from FLT_MIN to FLT_MAX.
int gblRandGeometric(float p)
Generates a random integer using the current random number generator over a Geometric distribution.
void gblSeedRand(uint8_t index, uint64_t seed)
Sets the seed associated with the given index to seed.
int gblRandLibC(void)
The builtin rand() generator from the C standard library.
int gblRandBinomial(int n, float p)
Generates a random integer using the current random number generator over a Binomial distribution.
int gblRand(void)
Returns a random number between 0 and RAND_MAX using the current random number generator.
float gblRandStudent(int n)
Generates a random integer using the current random number generator over a Student distribution.
float gblRandUniform(float a, float b)
Generates a random float using the current random number generator over the given range [a,...
int(* GblRandomGeneratorFn)(void)
Function prototype for a custom random generator to be set with gblSetRand()
float gblRandNormal(float m, float s)
Generates a random integer using the current random number generator over a Normal distribution.
GblBool gblRandBool(void)
Generates a random boolean value using the current generator.
int gblRandBernoulli(float p)
Generates a random integer using the current random number generator over a Bernoulli distribution.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)