Etterna 0.74.4
Loading...
Searching...
No Matches
rngthing.h
1#include <random>
2
3using RandomGen = std::mt19937;
4extern RandomGen g_RandomNumberGenerator;
5
6void
7seed_lua_prng();
8
9inline auto
10random_up_to(RandomGen& rng, int limit) -> int
11{
12 RandomGen::result_type res = rng();
13 // Cutting off the incomplete [0,n) chunk at the max value makes the result
14 // more evenly distributed. -Kyz
15 RandomGen::result_type up_to_max =
16 RandomGen::max() - (RandomGen::max() % limit);
17 while (res > up_to_max) {
18 res = rng();
19 }
20
21 return static_cast<int>(res % limit);
22}
23
24inline auto
25random_up_to(int limit) -> int
26{
27 return random_up_to(g_RandomNumberGenerator, limit);
28}
29
30inline auto
31RandomFloat() -> float
32{
33 return static_cast<float>(g_RandomNumberGenerator() / 4294967296.0);
34}
35
36inline auto
37RandomFloat(float fLow, float fHigh) -> float
38{
39 // SCALE() but l1 = 0 and h1 = 1 so simplifies to this
40 return (RandomFloat() * (fHigh - fLow)) + fLow;
41}
42
43inline auto
44RandomInt(int low, int high) -> int
45{
46 return random_up_to(g_RandomNumberGenerator, high - low + 1) + low;
47}
48
49inline auto
50RandomInt(int n) -> int
51{
52 return random_up_to(g_RandomNumberGenerator, n);
53}
54
55inline auto
56RandomInt(RandomGen& rng, int n) -> int
57{
58 return random_up_to(rng, n);
59}
60
61
62inline auto
63randomf(const float low = -1.F, const float high = 1.F) -> float
64{
65 return RandomFloat(low, high);
66}