Random.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "Random.h"
  9. void Random::Init()
  10. {
  11. std::random_device rd;
  12. Random::Seed(rd());
  13. }
  14. void Random::Seed(unsigned int seed)
  15. {
  16. sGenerator.seed(seed);
  17. }
  18. float Random::GetFloat()
  19. {
  20. return GetFloatRange(0.0f, 1.0f);
  21. }
  22. float Random::GetFloatRange(float min, float max)
  23. {
  24. std::uniform_real_distribution<float> dist(min, max);
  25. return dist(sGenerator);
  26. }
  27. int Random::GetIntRange(int min, int max)
  28. {
  29. std::uniform_int_distribution<int> dist(min, max);
  30. return dist(sGenerator);
  31. }
  32. Vector2 Random::GetVector(const Vector2& min, const Vector2& max)
  33. {
  34. Vector2 r = Vector2(GetFloat(), GetFloat());
  35. return min + (max - min) * r;
  36. }
  37. Vector3 Random::GetVector(const Vector3& min, const Vector3& max)
  38. {
  39. Vector3 r = Vector3(GetFloat(), GetFloat(), GetFloat());
  40. return min + (max - min) * r;
  41. }
  42. std::mt19937 Random::sGenerator;