MythTV  master
mythrandom.h
Go to the documentation of this file.
1 #ifndef MYTH_RANDOM_H_
2 #define MYTH_RANDOM_H_
3 
8 #include <cstdint>
9 #include <random>
10 
11 inline namespace MythRandomStd
12 {
13 
14 using MythRandomGenerator_32 = std::mt19937;
15 using MythRandomGenerator_64 = std::mt19937_64;
16 
20 inline uint32_t MythRandom()
21 {
22  static std::random_device rd;
23  static MythRandomGenerator_32 generator(rd());
24  return generator();
25 
26 }
27 
31 inline uint64_t MythRandom64()
32 {
33  static std::random_device rd;
34  static MythRandomGenerator_64 generator(rd());
35  return generator();
36 }
37 
45 inline uint32_t MythRandom(uint32_t min, uint32_t max)
46 {
47  static std::random_device rd;
48  static MythRandomGenerator_32 generator(rd());
49  std::uniform_int_distribution<uint32_t> distrib(min, max);
50  return distrib(generator);
51 }
52 
58 inline int MythRandomInt(int min, int max)
59 {
60  static std::random_device rd;
61  static MythRandomGenerator_32 generator(rd());
62  std::uniform_int_distribution<int> distrib(min, max);
63  return distrib(generator);
64 }
65 
75 inline bool rand_bool(uint32_t chance = 2)
76 {
77  if (chance < 2)
78  {
79  return true;
80  }
81  return MythRandom(0U, chance - 1) == 0U;
82 }
83 
84 } // namespace MythRandomStd
85 
86 #endif // MYTH_RANDOM_H_
MythRandomStd::rand_bool
bool rand_bool(uint32_t chance=2)
return a random bool with P(true) = 1/chance
Definition: mythrandom.h:75
MythRandomStd
Definition: mythrandom.h:11
MythRandomStd::MythRandomInt
int MythRandomInt(int min, int max)
generate a uniformly distributed random signed int in the closed interval [min, max].
Definition: mythrandom.h:58
MythRandomStd::MythRandomGenerator_64
std::mt19937_64 MythRandomGenerator_64
Definition: mythrandom.h:15
MythRandomStd::MythRandom64
uint64_t MythRandom64()
generate 64 random bits
Definition: mythrandom.h:31
MythRandomStd::MythRandomGenerator_32
std::mt19937 MythRandomGenerator_32
Definition: mythrandom.h:14
MythRandomStd::MythRandom
uint32_t MythRandom()
generate 32 random bits
Definition: mythrandom.h:20