random.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #include <cassert>
  3. #include <memory>
  4. #include <random/forwards.h>
  5. namespace engine::random {
  6. class Random {
  7. private:
  8. std::shared_ptr<Device> impl_;
  9. std::shared_ptr<Tape> tape_{nullptr};
  10. public:
  11. Random();
  12. template <typename P> Random(P const & p) : impl_(std::make_shared<P>(p)) {}
  13. template <typename P> Random(std::shared_ptr<P> const & p) : impl_(p) {}
  14. unsigned int exclusive(unsigned int max) const;
  15. double exclusive(double min, double max) const;
  16. double inclusive(double min, double max) const;
  17. std::shared_ptr<Device> device() const { return impl_; }
  18. /**
  19. * Create a scope in which all calls to the generator functions will record
  20. * their results into a tape. Expects that there is no tape currently set.
  21. * @param tape The tape to write to
  22. * @return A scope object that stops recording once it is descructed
  23. */
  24. auto record(std::shared_ptr<Tape> tape) {
  25. assert(tape && !tape_);
  26. tape_ = tape;
  27. auto cleanup = [this](void *) { tape_.reset(); };
  28. return std::unique_ptr<void, decltype(cleanup)>(this, cleanup);
  29. }
  30. };
  31. }