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
50} // 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
uint64_t get_random(sys::state const &state, uint32_t value_in)
Definition: prng.cpp:8
uint uint32_t
ulong uint64_t