-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.h
52 lines (44 loc) · 1.08 KB
/
random.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @file random.h
* @author DSN
* @brief Defines a namespace that encapsulates logic pertaining to the random generation of a number in a given range.
* @version 0.1
* @date 2023-08-18
*
*/
#ifndef RANDOM_H
#define RANDOM_H
#include <random>
#include <chrono>
namespace Random
{
/**
* @brief Generates a Mersenne Twister object
*
* @return std::mt19937
*/
inline std::mt19937 generate()
{
std::random_device rd{};
std::seed_seq ss{
static_cast<std::seed_seq::result_type>(std::chrono::steady_clock::now().time_since_epoch().count()),
rd(), rd(), rd()
};
return std::mt19937{ ss };
}
inline std::mt19937 mt{ generate() };
/**
* @brief Generates a number in the range \c [min,max]
*
* @details The random variable follows a uniform distribution.
*
* @param min
* @param max
* @return std::size_t
*/
inline std::size_t get(std::size_t min, std::size_t max)
{
return std::uniform_int_distribution{min, max}(mt);
}
}
#endif