2 #ifndef ENIGMA_RANDOM_H
3 #define ENIGMA_RANDOM_H
13 ENIGMA_STATIC_CLASS(
Random);
23 static typename std::enable_if<std::is_floating_point<T>::value,
T>::type
25 ENIGMA_ASSERT(min < max,
"min random value should be less than max value");
26 std::uniform_real_distribution<T> dist(min, max);
27 return dist(m_engine);
37 static typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value,
T>::type
39 ENIGMA_ASSERT(min < max,
"min random value should be less than max value");
40 std::uniform_int_distribution<T> dist(min, max);
41 return dist(m_engine);
49 static bool Bool(
const double p = 0.5) {
50 std::bernoulli_distribution dist(p);
51 return !!dist(m_engine);
59 static std::string
Str(
const std::size_t length) noexcept {
60 static std::random_device seed{};
61 static std::default_random_engine engine{seed()};
62 static std::uniform_int_distribution<short> choice(0, 2);
63 static std::uniform_int_distribution<int> lowercaseAlpha(
'a',
'z');
64 static std::uniform_int_distribution<int> uppercaseAlpha(
'A',
'Z');
65 static std::uniform_int_distribution<int> digits(
'0',
'9');
67 std::string str(length,
'\000');
69 switch (choice(engine)) {
71 c = lowercaseAlpha(engine);
74 c = uppercaseAlpha(engine);
90 static std::vector<byte>
Bytes(
const std::size_t length) noexcept {
91 std::vector<byte> bytes(length);
93 using random_bytes_engine = std::independent_bits_engine<std::default_random_engine, CHAR_BIT, byte>;
94 static random_bytes_engine gen{m_engine};
95 std::generate(bytes.begin(), bytes.end(), std::ref(gen));
98 std::uniform_int_distribution<std::uint16_t> dist{
static_cast<std::uint16_t
>(std::numeric_limits<byte>::min()),
static_cast<std::uint16_t
>(std::numeric_limits<byte>::max())};
99 std::generate(bytes.begin(), bytes.end(), [&]() ->
byte {
100 return dist(m_engine);
109 m_engine.seed(m_seed());
113 inline static std::random_device m_seed{};
114 inline static std::default_random_engine m_engine{m_seed()};
#define NS_ENIGMA_BEGIN
Enable/Disable Assertions.
#define ENIGMA_ASSERT(x,...)
Asserts.
static bool Bool(const double p=0.5)
Generates a random boolean value.
static std::vector< byte > Bytes(const std::size_t length) noexcept
Generates a random vector of bytes.
static std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, T >::type Int(const T min, const T max)
Generates a random integer between a range.
static std::enable_if< std::is_floating_point< T >::value, T >::type Real(const T min, const T max)
Generates a random real between a range.
static std::string Str(const std::size_t length) noexcept
Generates a random string.