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.

118 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());
  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. seed(s0, s1);
  25. }
  26. void seed(uint64_t s0 = 1, uint64_t s1 = 0) {
  27. state[0] = s0;
  28. state[1] = s1;
  29. operator()();
  30. }
  31. static uint64_t rotl(const uint64_t x, int k) {
  32. return (x << k) | (x >> (64 - k));
  33. }
  34. uint64_t operator()() {
  35. const uint64_t s0 = state[0];
  36. uint64_t s1 = state[1];
  37. const 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() {
  44. return 0;
  45. }
  46. constexpr uint64_t max() {
  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. // Easy random API
  71. extern thread_local Xoroshiro128Plus rng;
  72. /** Initializes the thread-local RNG state.
  73. Must call per-thread, otherwise the RNG will always return 0.
  74. */
  75. void init();
  76. /** Returns a uniform random uint64_t from 0 to UINT64_MAX */
  77. inline uint64_t u64() {
  78. return rng.u64();
  79. }
  80. /** Returns a uniform random uint32_t from 0 to UINT32_MAX */
  81. inline uint32_t u32() {
  82. return rng.u32();
  83. }
  84. /** Returns a uniform random float in the interval [0.0, 1.0) */
  85. inline float uniform() {
  86. return rng.f32();
  87. }
  88. /** Returns a normal random number with mean 0 and standard deviation 1 */
  89. float normal();
  90. /** Fills an array with random bytes. */
  91. void buffer(uint8_t* out, size_t len);
  92. /** Creates a vector of random bytes. */
  93. std::vector<uint8_t> vector(size_t len);
  94. } // namespace random
  95. } // namespace rack