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.

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