distribution.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // distribution.h
  3. // shared_random_generator
  4. //
  5. // Created by Sam Jaffe on 3/25/23.
  6. // Copyright © 2023 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <iosfwd>
  10. #include <type_traits>
  11. #include <random/forwards.h>
  12. namespace engine::random {
  13. template <typename Rand> class Distribution {
  14. public:
  15. using result_type = Rand;
  16. virtual ~Distribution() = default;
  17. virtual result_type operator()(Device & device) const = 0;
  18. // Allow us to operator on rvalue Devices as well
  19. result_type operator()(Device && device) const { return (*this)(device); }
  20. private:
  21. virtual void describe(std::ostream & os) const {}
  22. friend std::ostream & operator<<(std::ostream & os,
  23. Distribution const & dist) {
  24. dist.describe(os);
  25. return os;
  26. }
  27. };
  28. template <typename Rand> class Uniform : public Distribution<Rand> {
  29. public:
  30. using result_type = typename Distribution<Rand>::result_type;
  31. Uniform() : Uniform(0) {}
  32. Uniform(Rand min);
  33. Uniform(Rand min, Rand max);
  34. result_type operator()(Device & device) const override;
  35. private:
  36. void describe(std::ostream &) const override;
  37. private:
  38. Rand min_;
  39. Rand max_;
  40. };
  41. class Index : public Uniform<size_t> {
  42. public:
  43. explicit Index(size_t size) : Uniform(0, size - 1) {}
  44. };
  45. class Boolean : public Uniform<int> {
  46. public:
  47. Boolean() : Uniform(0, 1) {}
  48. void describe(std::ostream &os) const override;
  49. };
  50. }