Random.cpp 570 B

123456789101112131415161718192021222324252627282930
  1. #include <chrono>
  2. #include "common/utils/Random.h"
  3. Random::Random() : seed(std::chrono::steady_clock::now().time_since_epoch().count()) {
  4. }
  5. Random::Random(u64 seed) : seed(seed) {
  6. }
  7. u32 Random::next() {
  8. return nextSeed() >> 24;
  9. }
  10. u32 Random::next(uint bound) {
  11. return next() % bound;
  12. }
  13. float Random::nextFloat() {
  14. return next() * (1.0f / (0xFFFFFF + 1.0f));
  15. }
  16. float Random::nextFloat(float scale) {
  17. return (nextFloat() - 0.5f) * 2.0f * scale;
  18. }
  19. u64 Random::nextSeed() {
  20. seed = (seed * 0x5DEECE66DL + 0xBL) & 0xFFFFFFFFFFFF;
  21. return seed;
  22. }