panga
RandomWrapper.h
1 //-------------------------------------------------------------------------------------------------------
2 // Copyright (C) Taylor Woll and panga contributors. All rights reserved.
3 // Licensed under the MIT license. See LICENSE.txt file in the project root for
4 // full license information.
5 //-------------------------------------------------------------------------------------------------------
6 
7 #ifndef RANDOMWRAPPER_H__
8 #define RANDOMWRAPPER_H__
9 
10 #include <cstddef>
11 #include <cstdint>
12 #include <optional>
13 #include <random>
14 
15 namespace panga {
16 
21  public:
22  RandomWrapper() = default;
23  RandomWrapper(const RandomWrapper& rhs) = delete;
24  RandomWrapper& operator=(const RandomWrapper& rhs) = delete;
25  ~RandomWrapper() = default;
26 
31  template <typename IntegerType = uint32_t>
32  IntegerType RandomInteger(IntegerType min, IntegerType max) {
33  std::uniform_int_distribution<IntegerType> dist(min, max);
34  return dist(Engine());
35  }
36 
41  template <typename FloatType = double>
42  FloatType RandomFloat(FloatType min, FloatType max) {
43  std::uniform_real_distribution<FloatType> dist(min, max);
44  return dist(Engine());
45  }
46 
51  bool CoinFlip(double probability);
52 
56  std::byte RandomByte();
57 
58  protected:
59  std::mt19937& Engine();
60 
61  private:
62  std::optional<std::mt19937> engine_;
63 };
64 
65 } // namespace panga
66 
67 #endif // RANDOMWRAPPER_H__
panga::RandomWrapper::RandomByte
std::byte RandomByte()
Definition: RandomWrapper.cc:23
panga::RandomWrapper::RandomFloat
FloatType RandomFloat(FloatType min, FloatType max)
Definition: RandomWrapper.h:42
panga::RandomWrapper::CoinFlip
bool CoinFlip(double probability)
Definition: RandomWrapper.cc:18
panga::RandomWrapper::RandomInteger
IntegerType RandomInteger(IntegerType min, IntegerType max)
Definition: RandomWrapper.h:32
panga::RandomWrapper
Definition: RandomWrapper.h:20