| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- //
- // distribution.h
- // shared_random_generator
- //
- // Created by Sam Jaffe on 3/25/23.
- // Copyright © 2023 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include <iosfwd>
- #include <type_traits>
- #include <random/forwards.h>
- namespace engine::random {
- template <typename Rand> class Distribution {
- public:
- using result_type = Rand;
- virtual ~Distribution() = default;
- virtual result_type operator()(Device & device) const = 0;
- // Allow us to operator on rvalue Devices as well
- result_type operator()(Device && device) const { return (*this)(device); }
- private:
- virtual void describe(std::ostream & os) const {}
- friend std::ostream & operator<<(std::ostream & os,
- Distribution const & dist) {
- dist.describe(os);
- return os;
- }
- };
- template <typename Rand> class Uniform : public Distribution<Rand> {
- public:
- using result_type = typename Distribution<Rand>::result_type;
- Uniform() : Uniform(0) {}
- Uniform(Rand min);
- Uniform(Rand min, Rand max);
- result_type operator()(Device & device) const override;
- private:
- void describe(std::ostream &) const override;
- private:
- Rand min_;
- Rand max_;
- };
- class Index : public Uniform<size_t> {
- public:
- explicit Index(size_t size) : Uniform(0, size - 1) {}
- };
- class Boolean : public Uniform<int> {
- public:
- Boolean() : Uniform(0, 1) {}
- void describe(std::ostream &os) const override;
- };
- }
|