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.

116 lines
2.4KB

  1. #pragma once
  2. #include <common.hpp>
  3. #include <random>
  4. #include <vector>
  5. namespace rack {
  6. /** Random number generator
  7. */
  8. namespace random {
  9. /** xoroshiro128+. Very fast, not-cryptographic random number generator.
  10. From https://prng.di.unimi.it/
  11. Example:
  12. std::random_device rd;
  13. random::Xoroshiro128Plus rng(rd(), rd());
  14. uint64_t r = rng();
  15. uint32_t r = rng.u32();
  16. std::uniform_real_distribution<float> uniform(0.f, 1.f);
  17. float r = uniform(rng);
  18. std::normal_distribution<> normal(0.0, 1.0);
  19. double r = normal(rng);
  20. */
  21. struct Xoroshiro128Plus {
  22. uint64_t state[2];
  23. Xoroshiro128Plus(uint64_t s0 = 1, uint64_t s1 = 0) {
  24. state[0] = s0;
  25. state[1] = s1;
  26. // A bad seed will give a bad first result, so shift the state
  27. operator()();
  28. }
  29. static uint64_t rotl(uint64_t x, int k) {
  30. return (x << k) | (x >> (64 - k));
  31. }
  32. uint64_t operator()() {
  33. uint64_t s0 = state[0];
  34. uint64_t s1 = state[1];
  35. uint64_t result = s0 + s1;
  36. s1 ^= s0;
  37. state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14);
  38. state[1] = rotl(s1, 36);
  39. return result;
  40. }
  41. constexpr uint64_t min() const {
  42. return 0;
  43. }
  44. constexpr uint64_t max() const {
  45. return UINT64_MAX;
  46. }
  47. uint64_t u64() {
  48. return operator()();
  49. }
  50. uint64_t u32() {
  51. // Take top 32 bits which has better randomness properties.
  52. return u64() >> 32;
  53. }
  54. uint16_t u16() {
  55. return u64() >> 48;
  56. }
  57. uint8_t u8() {
  58. return u64() >> 56;
  59. }
  60. float f32() {
  61. // The multiplier is 2f7fffff in hex. This gives maximum precision of uint32_t -> float conversion and its image is [0, 1).
  62. return u32() * 2.32830629e-10f;
  63. }
  64. float f64() {
  65. return u64() * 5.421010862427522e-20;
  66. }
  67. };
  68. // Easy random API
  69. extern thread_local Xoroshiro128Plus rng;
  70. /** Initializes the thread-local RNG state.
  71. Must call when creating a thread, otherwise random state is undefined (might always return 0).
  72. */
  73. void init();
  74. /** Returns a uniform random uint64_t from 0 to UINT64_MAX */
  75. inline uint64_t u64() {
  76. return rng.u64();
  77. }
  78. /** Returns a uniform random uint32_t from 0 to UINT32_MAX */
  79. inline uint32_t u32() {
  80. return rng.u32();
  81. }
  82. /** Returns a uniform random float in the interval [0.0, 1.0) */
  83. inline float uniform() {
  84. return rng.f32();
  85. }
  86. /** Returns a normal random number with mean 0 and standard deviation 1 */
  87. float normal();
  88. /** Fills an array with random bytes. */
  89. void buffer(uint8_t* out, size_t len);
  90. /** Creates a vector of random bytes. */
  91. std::vector<uint8_t> vector(size_t len);
  92. } // namespace random
  93. } // namespace rack