Random.cpp 1.2 KB

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