1 #ifndef INCLUDE_AL_UTIL_MATH_RANDOM_HPP
2 #define INCLUDE_AL_UTIL_MATH_RANDOM_HPP
45 #include "al/math/al_StdRandom.hpp"
53 #include "al/math/al_Constants.hpp"
54 #include "al/types/al_Conversion.hpp"
68 inline static uint32_t seed() {
69 static uint32_t val = time(NULL);
70 return val = val * 1664525UL + 1013904223UL;
76 template <
class RNG = al::rnd::Tausworthe>
92 RNG&
rng() {
return mRNG; }
95 float uniform() {
return al::uintToUnit<float>(mRNG()); }
106 return T((hi - lo) *
uniform()) + lo;
110 float uniformS() {
return al::uintToUnitS<float>(mRNG()); }
123 template <
int N,
class T>
127 template <
template <
int,
class>
class VecType,
int N,
class T>
128 void ball(VecType<N, T>& point) {
133 template <
class VecType>
149 void normal(T& y1, T& y2);
161 float sign(
float x = 1.f);
164 bool prob() {
return mRNG() & 0x80000000; }
171 void shuffle(T* arr, uint32_t len);
174 float gaussian() {
return normal(); }
176 void gaussian(T& y1, T& y2) {
199 seed(al::rnd::seed());
210 void seed(uint32_t v) { mVal = v; }
248 seed(al::rnd::seed());
259 void seed(uint32_t v) { mVal = v; }
311 void seed(uint32_t v);
314 void seed(uint32_t v1, uint32_t v2, uint32_t v3, uint32_t v4);
317 uint32_t s1, s2, s3, s4;
328 return s1 ^ s2 ^ s3 ^ s4;
334 seed(g(), g(), g(), g());
340 v1& 0xffffffe ? s1 = v1 : s1 = ~v1;
341 v2& 0xffffff8 ? s2 = v2 : s2 = ~v2;
342 v3& 0xffffff0 ? s3 = v3 : s3 = ~v3;
343 v4& 0xfffff80 ? s4 = v4 : s4 = ~v4;
346 inline void Tausworthe::iterate() {
347 s1 = ((s1 & 0xfffffffe) << 18) ^ (((s1 << 6) ^ s1) >> 13);
348 s2 = ((s2 & 0xfffffff8) << 2) ^ (((s2 << 2) ^ s2) >> 27);
349 s3 = ((s3 & 0xfffffff0) << 7) ^ (((s3 << 13) ^ s3) >> 21);
350 s4 = ((s4 & 0xffffff80) << 13) ^ (((s4 << 3) ^ s4) >> 12);
354 template <
int N,
class T>
359 for (
int i = 0; i < N; ++i) {
382 w = x1 * x1 + x2 * x2;
386 w = std::sqrt((-2.f * std::log(w)) / w);
397 u.i = 0x3f800000 | (mRNG() >> 9);
398 v.i = 0x3f800000 | (mRNG() >> 9);
408 u.i |= mRNG() & 0x80000000;
416 for (uint32_t i = len - 1; i > 0; --i) {
434 StdRnd(
unsigned int seed) : mUniformf{seed}, mUniformi{seed} {}
437 float operator()() {
return mUniformf(); }
438 float operator()(
float hi) {
return mUniformf(0, hi); }
439 float operator()(
float lo,
float hi) {
return mUniformf(lo, hi); }
441 void seed(
unsigned int s) { mUniformf.seed(s); }
443 int randi(
int hi) {
return mUniformi(0, hi); }
444 int randi(
int lo,
int hi) {
return mUniformi(lo, hi); }
446 void seedi(
unsigned int s) { mUniformi.seed(s); }
459 template <
typename T>
461 return static_cast<T
>(mRNG(
float(hi)));
465 template <
typename T>
467 return static_cast<T
>(mRNG(
float(lo),
float(hi)));
472 return mRNG(-1.0f, 1.0f);
476 template <
typename T>
478 return static_cast<T
>(mRNG(
float(-lim),
float(lim)));
488 static float arr[2] = {-1.0f, 1.0f};
489 return x * arr[mRNG.randi(0, 1)];
495 for (uint32_t i = len - 1; i > 0; i -= 1) {
496 uint32_t j = mRNG.randi(i + 1);
Linear congruential uniform pseudo-random number generator.
uint32_t operator()()
Generate next uniform random integer in [0, 2^32)
LinCon()
Default constructor uses a randomly generated seed.
void seed(uint32_t v)
Set seed.
void type(int v)
Change the type of equation used.
Multiplicative linear congruential uniform pseudo-random number generator.
uint32_t operator()()
Generate next uniform random integer in [0, 2^32)
void seed(uint32_t v)
Set seed.
void type(int v)
Change the type of equation used.
MulLinCon()
Default constructor uses a randomly generated seed.
float normal()
Returns standard normal variate.
Random & seed(uint32_t v)
Set seed.
bool prob()
Returns true with a probability of 0.5.
float triangle()
Returns triangle distribution variate, in (-1,1)
RNG & rng()
Get underlying random number generator.
void ball(VecType< N, T > &point)
Returns point within a unit ball.
VecType ball()
Returns point within a unit ball.
T uniform(const T &hi)
Returns uniform random in [0, hi)
T uniform(const T &hi, const T &lo)
Returns uniform random in [lo, hi)
float uniformS()
Returns uniform random in [-1, 1)
float sign(float x=1.f)
Returns argument with sign randomly flipped.
void shuffle(T *arr, uint32_t len)
Randomly shuffles elements in array.
T uniformS(const T &lim)
Returns uniform random in [-lim, lim)
float uniform()
Returns uniform random in [0, 1)
Random()
Default constructor uses a randomly generated seed.
T triangle(const T &hi)
Returns triangle distribution variate, in (-hi,hi)
bool prob(float p)
Returns true with a probability of p.
Combined Tausworthe uniform pseudo-random number generator.
Tausworthe()
Default constructor uses a randomly generated seed.
void seed(uint32_t v)
Set seed.
uint32_t operator()()
Generate next uniform random integer in [0, 2^32)
float uniformS()
Returns signed uniform random in (-1, 1)
float uniform()
Returns uniform random in [0, 1)