Project Alice
Loading...
Searching...
No Matches
prng.cpp
Go to the documentation of this file.
1#include "prng.hpp"
2#include "system_state.hpp"
3
4#include "random123/philox.h"
5
6namespace rng {
7
8uint64_t get_random(sys::state const& state, uint32_t value_in) { // try to populate as many bits of value_in as you can
9 r123::Philox4x32 rng;
10 r123::Philox4x32::ctr_type c = {state.current_date.value, value_in, 0, 0 };
11 r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23};
12
13 r123::Philox4x32::ctr_type r = rng(c, k);
14
15 return (uint64_t(r[0]) << 32) | uint64_t(r[1]);
16}
17uint64_t get_random(sys::state const& state, uint32_t value_in_hi, uint32_t value_in_lo) {
18 r123::Philox4x32 rng;
19 r123::Philox4x32::ctr_type c = {value_in_hi, value_in_lo, 0, 0};
20 r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23 };
21
22 r123::Philox4x32::ctr_type r = rng(c, k);
23
24 return (uint64_t(r[0]) << 32) | uint64_t(r[1]);
25}
26random_pair get_random_pair(sys::state const& state, uint32_t value_in) { // each call natively generates 128 random bits anyways
27
28 r123::Philox4x32 rng;
29 r123::Philox4x32::ctr_type c = {state.current_date.value, value_in, 0, 0 };
30 r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23 };
31
32 r123::Philox4x32::ctr_type r = rng(c, k);
33
34 return random_pair{(uint64_t(r[0]) << 32) | uint64_t(r[1]), (uint64_t(r[2]) << 32) | uint64_t(r[3])};
35}
36random_pair get_random_pair(sys::state const& state, uint32_t value_in_hi, uint32_t value_in_lo) { // each call natively generates 128 random bits anyways
37
38 r123::Philox4x32 rng;
39 r123::Philox4x32::ctr_type c = {value_in_hi, value_in_lo, 0, 0 };
40 r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23 };
41
42 r123::Philox4x32::ctr_type r = rng(c, k);
43
44 return random_pair{(uint64_t(r[0]) << 32) | uint64_t(r[1]), (uint64_t(r[2]) << 32) | uint64_t(r[3])};
45}
46uint32_t reduce(uint32_t value_in, uint32_t upper_bound) {
47 return uint32_t((uint64_t(value_in) * uint64_t(upper_bound)) >> 32);
48}
49
50float get_random_float(sys::state const& state, uint32_t value_in) {
51 r123::Philox4x32 rng;
52 r123::Philox4x32::ctr_type c = {state.current_date.value, value_in, 0, 0 };
53 r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23};
54 r123::Philox4x32::ctr_type r = rng(c, k);
55 //
56 union {
57 uint32_t u;
58 float f;
59 } pattern;
60 pattern.u = 0x3f800000;
61 pattern.u |= 0x7fffff & r[1];
62 return pattern.f - 1.0f;
63}
64
65float get_random_float(sys::state const& state, uint32_t value_in_hi, uint32_t value_in_lo) {
66 r123::Philox4x32 rng;
67 r123::Philox4x32::ctr_type c = {value_in_hi, value_in_lo, 0, 0 };
68 r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23};
69 r123::Philox4x32::ctr_type r = rng(c, k);
70 //
71 union {
72 uint32_t u;
73 float f;
74 } pattern;
75 pattern.u = 0x3f800000;
76 pattern.u |= 0x7fffff & r[1];
77 return pattern.f - 1.0f;
78}
79
80} // namespace rng
Definition: prng.cpp:6
random_pair get_random_pair(sys::state const &state, uint32_t value_in)
Definition: prng.cpp:26
uint32_t reduce(uint32_t value_in, uint32_t upper_bound)
Definition: prng.cpp:46
float get_random_float(sys::state const &state, uint32_t value_in)
Definition: prng.cpp:50
uint64_t get_random(sys::state const &state, uint32_t value_in)
Definition: prng.cpp:8
uint uint32_t
ulong uint64_t
Holds important data about the game world, state, and other data regarding windowing,...