Random.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <chrono>
  2. #include <iostream>
  3. #include "utils/Random.h"
  4. Random::Random(int seed) : index(0) {
  5. for(int i = 0; i < N; i++) {
  6. data[i] = seed;
  7. seed = seed * 7 + 31;
  8. }
  9. }
  10. Random::Random() : Random(std::chrono::steady_clock::now().time_since_epoch().count()) {
  11. }
  12. void Random::update() {
  13. static const int map[2] = {0, -1900031960};
  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. int r = data[index++];
  27. r ^= (r << 7) & 0x2B5B2500;
  28. r ^= (r << 15) & 0xDB8B0000;
  29. r ^= (r >> 16);
  30. return r & static_cast<int> (-1u >> 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. }