diff options
author | jbzdarkid <jbzdarkid@gmail.com> | 2019-11-19 09:42:45 -0800 |
---|---|---|
committer | jbzdarkid <jbzdarkid@gmail.com> | 2019-11-19 09:42:45 -0800 |
commit | 52b01775c2c94f08786696cae7836ca56f683739 (patch) | |
tree | d6d7d7604c8f5d793be28bcf21753b02d7d31bb7 /Source/Random.cpp | |
parent | 7a13ef0a94f6ed4989677530b1b37e1af0692fc7 (diff) | |
download | witness-tutorializer-52b01775c2c94f08786696cae7836ca56f683739.tar.gz witness-tutorializer-52b01775c2c94f08786696cae7836ca56f683739.tar.bz2 witness-tutorializer-52b01775c2c94f08786696cae7836ca56f683739.zip |
Fix RNG -- this will break seeds, though.
Diffstat (limited to 'Source/Random.cpp')
-rw-r--r-- | Source/Random.cpp | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/Source/Random.cpp b/Source/Random.cpp index 61fd30f..d9fe678 100644 --- a/Source/Random.cpp +++ b/Source/Random.cpp | |||
@@ -1,7 +1,7 @@ | |||
1 | #include <chrono> | 1 | #include <chrono> |
2 | #include "Random.h" | 2 | #include "Random.h" |
3 | 3 | ||
4 | int Random::s_seed = static_cast<int>(time(nullptr)); // Seed from the time in milliseconds | 4 | uint32_t Random::s_seed = static_cast<int>(time(nullptr)); // Seed from the time in milliseconds |
5 | 5 | ||
6 | void Random::SetSeed(int seed) { | 6 | void Random::SetSeed(int seed) { |
7 | s_seed = seed; | 7 | s_seed = seed; |
@@ -9,6 +9,7 @@ void Random::SetSeed(int seed) { | |||
9 | 9 | ||
10 | // Returns a random integer in [min, max] | 10 | // Returns a random integer in [min, max] |
11 | int Random::RandInt(int min, int max) { | 11 | int Random::RandInt(int min, int max) { |
12 | s_seed = (214013 * s_seed + 2531011) % 2147483648; | 12 | s_seed = (214013 * s_seed + 2531011); // Implicit unsigned integer overflow |
13 | return (s_seed % (max - min + 1)) + min; | 13 | int32_t maskedSeed = ((s_seed >> 16) & 0x7fff); // Only use bits 16-30 |
14 | return (maskedSeed % (max - min + 1)) + min; | ||
14 | } | 15 | } |