diff options
Diffstat (limited to 'source/base/base_rand.h')
| -rw-r--r-- | source/base/base_rand.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/source/base/base_rand.h b/source/base/base_rand.h new file mode 100644 index 0000000..bfdab0f --- /dev/null +++ b/source/base/base_rand.h | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #ifndef BASE_RAND_H | ||
| 2 | #define BASE_RAND_H | ||
| 3 | |||
| 4 | // source: https://rosettacode.org/wiki/Pseudo-random_numbers/Splitmix64 | ||
| 5 | |||
| 6 | #define RAND_CONSTANT 6364136223846793005 | ||
| 7 | #define GEN_RAND() _generate_random_u64(RAND_CONSTANT) | ||
| 8 | |||
| 9 | #endif /* BASE_RAND_H */ | ||
| 10 | |||
| 11 | #ifdef BASE_RAND_IMPLEMENTATION | ||
| 12 | internal u64 | ||
| 13 | generate_random_u64(u64 constant) | ||
| 14 | { | ||
| 15 | time_t current_time = time(0); | ||
| 16 | constant = current_time ^ constant; | ||
| 17 | |||
| 18 | constant += 0x9e3779b97f4a7c15; | ||
| 19 | |||
| 20 | u64 z = constant; | ||
| 21 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; | ||
| 22 | z = (z ^ (z >> 27)) * 0x94d049bb133111eb; | ||
| 23 | z = z ^ (z >> 31); | ||
| 24 | |||
| 25 | return z; | ||
| 26 | } | ||
| 27 | |||
| 28 | #endif | ||
