panann
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 <algorithm>
11 #include <cstddef>
12 #include <cstdint>
13 #include <optional>
14 #include <random>
15 #include <vector>
16 
17 namespace panann {
18 
23  public:
24  RandomWrapper() = default;
25  RandomWrapper(const RandomWrapper& rhs) = delete;
26  RandomWrapper& operator=(const RandomWrapper& rhs) = delete;
27  ~RandomWrapper() = default;
28 
33  template <typename IntegerType = uint32_t>
34  IntegerType RandomInteger(IntegerType min, IntegerType max) {
35  std::uniform_int_distribution<IntegerType> dist(min, max);
36  return dist(Engine());
37  }
38 
43  template <typename FloatType = double>
44  FloatType RandomFloat(FloatType min, FloatType max) {
45  std::uniform_real_distribution<FloatType> dist(min, max);
46  return dist(Engine());
47  }
48 
53  bool CoinFlip(double probability);
54 
58  std::byte RandomByte();
59 
63  template <typename T>
64  void ShuffleVector(std::vector<T>* vec) {
65  std::shuffle(vec->begin(), vec->end(), Engine());
66  }
67 
68  protected:
69  std::mt19937& Engine();
70 
71  private:
72  std::optional<std::mt19937> engine_;
73 };
74 
75 } // namespace panann
76 
77 #endif // RANDOMWRAPPER_H__
panann::RandomWrapper::RandomByte
std::byte RandomByte()
Definition: RandomWrapper.cc:23
panann::RandomWrapper::RandomFloat
FloatType RandomFloat(FloatType min, FloatType max)
Definition: RandomWrapper.h:44
panann::RandomWrapper
Definition: RandomWrapper.h:22
panann::RandomWrapper::CoinFlip
bool CoinFlip(double probability)
Definition: RandomWrapper.cc:18
panann::RandomWrapper::ShuffleVector
void ShuffleVector(std::vector< T > *vec)
Definition: RandomWrapper.h:64
panann::RandomWrapper::RandomInteger
IntegerType RandomInteger(IntegerType min, IntegerType max)
Definition: RandomWrapper.h:34