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.

38 lines
563B

  1. #include <random.hpp>
  2. #include <math.hpp>
  3. #include <system.hpp>
  4. namespace rack {
  5. namespace random {
  6. static Xoroshiro128Plus rng;
  7. void init() {
  8. // Don't reset state if already seeded
  9. if (rng.isSeeded())
  10. return;
  11. // Get epoch time for seed
  12. double time = system::getUnixTime();
  13. uint64_t sec = time;
  14. uint64_t nsec = std::fmod(time, 1.0) * 1e9;
  15. rng.seed(sec, nsec);
  16. // Shift state a few times due to low seed entropy
  17. for (int i = 0; i < 4; i++) {
  18. rng();
  19. }
  20. }
  21. Xoroshiro128Plus& local() {
  22. return rng;
  23. }
  24. } // namespace random
  25. } // namespace rack