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.

119 lines
2.5KB

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