| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- #include "random/random.h"
- #include <cassert>
- #include <cfloat>
- #include <random>
- #include <scope_guard/scope_guard.hpp>
- #include "random/device.h"
- #include "random/distribution.h"
- #include "random/tape.h"
- #define IMPLEMENT_RANDOM(T) \
- template T Random::operator()(Distribution<T> const &) const;
- namespace engine::random {
- class DefaultDevice : public Device {
- public:
- result_type operator()() final { return rng(); }
- private:
- std::mt19937 rng{std::random_device{}()};
- };
- Random::Random() : impl_(std::make_shared<DefaultDevice>()) {}
- Random::Random(Random const & other, std::shared_ptr<Tape> with_tape)
- : impl_(other.impl_), tape_(with_tape) {}
- Random::Random(Random const & other, Tape & with_tape)
- : impl_(other.impl_), tape_(&with_tape, [](void *) {}) {}
- template <typename Rand>
- Rand Random::operator()(Distribution<Rand> const & dist) const {
- return tape_ ? tape_->apply(dist, impl_->apply(dist)) : impl_->apply(dist);
- }
- scope_exit Random::record(std::shared_ptr<Tape> tape) {
- assert(tape && !tape_);
- tape_ = tape;
- return [this]() { tape_.reset(); };
- }
- IMPLEMENT_DEVICE(IMPLEMENT_RANDOM)
- }
|