Random.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <chrono>
  2. #include "utils/Random.h"
  3. Random::Random(Seed seed) : index(0) {
  4. for(int i = 0; i < N; i++) {
  5. data[i] = seed;
  6. seed = seed * 7 + 31;
  7. }
  8. }
  9. Random::Random()
  10. : Random(std::chrono::steady_clock::now().time_since_epoch().count()) {
  11. }
  12. void Random::update() {
  13. static const Seed map[2] = {0, 0x8EBFD028};
  14. for(int i = 0; i < N - M; i++) {
  15. data[i] = data[i + M] ^ (data[i] >> 1) ^ map[data[i] & 1];
  16. }
  17. for(int i = N - M; i < N; i++) {
  18. data[i] = data[i + (M - N)] ^ (data[i] >> 1) ^ map[data[i] & 1];
  19. }
  20. index = 0;
  21. }
  22. int Random::next() {
  23. if(index >= N) {
  24. update();
  25. }
  26. Seed r = data[index++];
  27. r ^= (r << 7) & 0x2B5B2500;
  28. r ^= (r << 15) & 0xDB8B0000;
  29. r ^= (r >> 16);
  30. return static_cast<int>(r >> 1);
  31. }
  32. int Random::next(int min, int inclusiveMax) {
  33. return min + next() % (inclusiveMax - min + 1);
  34. }
  35. float Random::nextFloat() {
  36. static constexpr int bits = sizeof(int) * 6;
  37. static constexpr int mask = (1 << bits) - 1;
  38. return (next() & mask) * (1.0f / (1.0f + mask));
  39. }
  40. float Random::nextFloat(float min, float exclusiveMax) {
  41. return min + nextFloat() * (exclusiveMax - min);
  42. }