random.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef __RANDOM_H
  2. #define __RANDOM_H
  3. #include <random>
  4. namespace rnd {
  5. struct Generator {
  6. std::mt19937_64 gen;
  7. size_t seed;
  8. template <typename T>
  9. void init(T _seed) {
  10. gen.seed(_seed);
  11. seed = _seed;
  12. }
  13. template <typename T>
  14. T range(T a, T b) {
  15. std::uniform_int_distribution<T> dist(a, b);
  16. return dist(gen);
  17. }
  18. template <typename T>
  19. T gauss(T mean, T stddev) {
  20. if (stddev == 0)
  21. return mean;
  22. std::normal_distribution<T> dist(mean, stddev);
  23. return dist(gen);
  24. }
  25. template <typename T>
  26. T uniform(T a, T b) {
  27. std::uniform_real_distribution<T> dist(a, b);
  28. return dist(gen);
  29. }
  30. unsigned int geometric(double p) {
  31. std::geometric_distribution<unsigned int> dist(p);
  32. return dist(gen);
  33. }
  34. template <typename T>
  35. T n(T n_) {
  36. return range((T)0, n_-1);
  37. }
  38. template <typename T>
  39. T biased_gauss(T mean, T stddev, T bias, T factor) {
  40. if (bias < 0)
  41. factor = -factor;
  42. mean += (stddev * factor * log((bias*bias) + 1.0));
  43. return gauss(mean, stddev);
  44. }
  45. };
  46. }
  47. #endif