You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
3.4KB

  1. #pragma once
  2. #include <common.hpp>
  3. #include <random>
  4. #include <vector>
  5. namespace rack {
  6. /** Random number generation */
  7. namespace random {
  8. /** xoroshiro128+. Very fast, not-cryptographic random number generator.
  9. From https://prng.di.unimi.it/
  10. Example:
  11. std::random_device rd;
  12. random::Xoroshiro128Plus rng(rd(), rd());
  13. uint64_t r = rng();
  14. std::uniform_real_distribution<float> uniform(0.f, 1.f);
  15. float r = uniform(rng);
  16. std::normal_distribution<> normal(0.0, 1.0);
  17. double r = normal(rng);
  18. */
  19. struct Xoroshiro128Plus {
  20. uint64_t state[2] = {};
  21. void seed(uint64_t s0, uint64_t s1) {
  22. state[0] = s0;
  23. state[1] = s1;
  24. // A bad seed will give a bad first result, so shift the state
  25. operator()();
  26. }
  27. bool isSeeded() {
  28. return state[0] || state[1];
  29. }
  30. static uint64_t rotl(uint64_t x, int k) {
  31. return (x << k) | (x >> (64 - k));
  32. }
  33. uint64_t operator()() {
  34. uint64_t s0 = state[0];
  35. uint64_t s1 = state[1];
  36. uint64_t result = s0 + s1;
  37. s1 ^= s0;
  38. state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14);
  39. state[1] = rotl(s1, 36);
  40. return result;
  41. }
  42. constexpr uint64_t min() const {
  43. return 0;
  44. }
  45. constexpr uint64_t max() const {
  46. return UINT64_MAX;
  47. }
  48. };
  49. // Simple thread-local API
  50. /** Initializes the thread-local RNG state.
  51. Must call when creating a thread, otherwise random state is undefined (might always return 0).
  52. */
  53. void init();
  54. /** Returns the thread-local generator.
  55. */
  56. Xoroshiro128Plus& local();
  57. template <typename T>
  58. T get() {
  59. // Call operator()() and cast by default
  60. return local()();
  61. }
  62. template <>
  63. inline uint32_t get() {
  64. // Take top 32 bits which has better randomness properties.
  65. return get<uint64_t>() >> 32;
  66. }
  67. template <>
  68. inline uint16_t get() {
  69. return get<uint64_t>() >> 48;
  70. }
  71. template <>
  72. inline uint8_t get() {
  73. return get<uint64_t>() >> 56;
  74. }
  75. template <>
  76. inline bool get() {
  77. return get<uint64_t>() >> 63;
  78. }
  79. template <>
  80. inline float get() {
  81. // The multiplier is 2f7fffff in hex. This gives maximum precision of uint32_t -> float conversion and its image is [0, 1).
  82. return get<uint32_t>() * 2.32830629e-10f;
  83. }
  84. template <>
  85. inline double get() {
  86. return get<uint64_t>() * 5.421010862427522e-20;
  87. }
  88. /** Returns a uniform random uint64_t from 0 to UINT64_MAX */
  89. inline uint64_t u64() {return get<uint64_t>();}
  90. /** Returns a uniform random uint32_t from 0 to UINT32_MAX */
  91. inline uint32_t u32() {return get<uint32_t>();}
  92. /** Returns a uniform random float in the interval [0.0, 1.0) */
  93. inline float uniform() {return get<float>();}
  94. /** Returns a normal random number with mean 0 and standard deviation 1 */
  95. inline float normal() {
  96. // Box-Muller transform
  97. float radius = std::sqrt(-2.f * std::log(1.f - get<float>()));
  98. float theta = 2.f * M_PI * get<float>();
  99. return radius * std::sin(theta);
  100. // // Central Limit Theorem
  101. // const int n = 8;
  102. // float sum = 0.0;
  103. // for (int i = 0; i < n; i++) {
  104. // sum += get<float>();
  105. // }
  106. // return (sum - n / 2.f) / std::sqrt(n / 12.f);
  107. }
  108. /** Fills an array with random bytes. */
  109. inline void buffer(uint8_t* out, size_t len) {
  110. Xoroshiro128Plus& rng = local();
  111. for (size_t i = 0; i < len; i += 4) {
  112. uint64_t r = rng();
  113. out[i] = r;
  114. if (i + 1 < len)
  115. out[i + 1] = r >> 8;
  116. if (i + 2 < len)
  117. out[i + 2] = r >> 16;
  118. if (i + 3 < len)
  119. out[i + 3] = r >> 24;
  120. }
  121. }
  122. /** Creates a vector of random bytes. */
  123. inline std::vector<uint8_t> vector(size_t len) {
  124. std::vector<uint8_t> v(len);
  125. buffer(v.data(), len);
  126. return v;
  127. }
  128. } // namespace random
  129. } // namespace rack