MythTV master
mythrandom.h
Go to the documentation of this file.
1#ifndef MYTH_RANDOM_H_
2#define MYTH_RANDOM_H_
8#include <cstdint>
9#include <random>
10
11inline namespace MythRandomStd
12{
13
14using MythRandomGenerator_32 = std::mt19937;
15using MythRandomGenerator_64 = std::mt19937_64;
16
20inline uint32_t MythRandom()
21{
22 static std::random_device rd;
23 static MythRandomGenerator_32 generator(rd());
24 return generator();
25
26}
27
31inline uint64_t MythRandom64()
32{
33 static std::random_device rd;
34 static MythRandomGenerator_64 generator(rd());
35 return generator();
36}
37
45inline 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
58inline 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
75inline 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_
uint64_t MythRandom64()
generate 64 random bits
Definition: mythrandom.h:31
int MythRandomInt(int min, int max)
generate a uniformly distributed random signed int in the closed interval [min, max].
Definition: mythrandom.h:58
std::mt19937_64 MythRandomGenerator_64
Definition: mythrandom.h:15
uint32_t MythRandom()
generate 32 random bits
Definition: mythrandom.h:20
std::mt19937 MythRandomGenerator_32
Definition: mythrandom.h:14
bool rand_bool(uint32_t chance=2)
return a random bool with P(true) = 1/chance
Definition: mythrandom.h:75