mersenne32.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. class IInputStream;
  4. namespace NPrivate {
  5. class TMersenne32 {
  6. static constexpr int N = 624;
  7. public:
  8. inline TMersenne32(ui32 s = 19650218UL) noexcept
  9. : mti(N + 1)
  10. {
  11. InitGenRand(s);
  12. }
  13. inline TMersenne32(const ui32* init_key, size_t key_length) noexcept
  14. : mti(N + 1)
  15. {
  16. InitByArray(init_key, key_length);
  17. }
  18. TMersenne32(IInputStream& input);
  19. inline ui32 GenRand() noexcept {
  20. if (mti >= N) {
  21. InitNext();
  22. }
  23. ui32 y = mt[mti++];
  24. y ^= (y >> 11);
  25. y ^= (y << 7) & 0x9d2c5680UL;
  26. y ^= (y << 15) & 0xefc60000UL;
  27. y ^= (y >> 18);
  28. return y;
  29. }
  30. private:
  31. void InitNext() noexcept;
  32. void InitGenRand(ui32 s) noexcept;
  33. void InitByArray(const ui32* init_key, size_t key_length) noexcept;
  34. private:
  35. ui32 mt[N];
  36. int mti;
  37. };
  38. }