123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- #include "utils/Random.hpp"
- Core::Random::Random(Seed seed) : index(0) {
- for(int i = 0; i < N; i++) {
- data[i] = seed;
- seed = seed * 7 + 31;
- }
- }
- void Core::Random::update() {
- static const Seed map[2] = {0, 0x8EBFD028};
- for(int i = 0; i < N - M; i++) {
- data[i] = data[i + M] ^ (data[i] >> 1) ^ map[data[i] & 1];
- }
- for(int i = N - M; i < N; i++) {
- data[i] = data[i + (M - N)] ^ (data[i] >> 1) ^ map[data[i] & 1];
- }
- index = 0;
- }
- int Core::Random::next() {
- if(index >= N) {
- update();
- }
- Seed r = data[index++];
- r ^= (r << 7) & 0x2B5B2500;
- r ^= (r << 15) & 0xDB8B0000;
- r ^= (r >> 16);
- return static_cast<int>(r >> 1);
- }
- int Core::Random::next(int min, int inclusiveMax) {
- return min + next() % (inclusiveMax - min + 1);
- }
- bool Core::Random::nextBool() {
- return next() & 1;
- }
- float Core::Random::nextFloat() {
- static constexpr i32 m = 0x7FFFFFFF;
- float f = static_cast<float>(next() & m) * (1.0f / static_cast<float>(m));
- return f >= 1.0f ? nextFloat() : f;
- }
- float Core::Random::nextFloat(float min, float exclusiveMax) {
- return min + nextFloat() * (exclusiveMax - min);
- }
|