-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomNumberGenerators.h
91 lines (76 loc) · 2.95 KB
/
randomNumberGenerators.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* File: randomNumberGenerators.h
* Author: massimo
*
* Created on November 21, 2017, 6:57 PM
*/
#pragma once
#include <random>
////////////////////////////////////////////////////////////////////////////////
namespace utilities {
// Seed a random number engine with greater unpredictability.
//
// A pseudo-random number engine is initialized with a seed, which
// determines the initial state of the engine. When seeding with
// a single 32 bit value (as is typically provided by
// [std::random_device](cpp/numeric/random/random_device)), the
// engine can be in one of only 2^32 states. By providing an engine with
// a greater amount of random seed data, we increase the number of possible
// states and therefore the unpredictability of the engine.
//
// With std::random_device rd;, we create a `std::random_device` as a source of
// non-deterministic random numbers (if such a source is available).
// With std::seed_seq seed_seq {rd(), rd(), rd(), rd(), rd(), rd()};
// we initialize a [`std::seed_seq`](cpp/numeric/random/seed_seq)
// with several initial seeds produced by the `std::random_device`.
// With static std::mt19937 gen {seed_seq}; we seed a random number engine with
// this `std::seed_seq`, which provides the engine with a sequence of evenly
// distributed seeds with low bias (although [not bias-free] (http://www.pcg-random.org/posts/cpp-seeding-surprises.html)).
// Providing more initial seeds will increase the unpredictability of
// the engine by increasing the number of possible seed sequences that
// might be generated.
//
// While a `std::seed_seq` can be used to initialize multiple random number
// engines, bear in mind that a particular `std::seed_seq` will produce the same
// sequence of seeds each time it is used.
//
// **Note**: An unpredictable random number engine is not necessarily
// cryptographically secure.
//
template <typename T = double>
const
T
getRandomFP(T min, T max) noexcept;
template <typename T>
const
T
getRandomFP(T min, T max) noexcept
{
static_assert(std::is_floating_point<T>::value != 0, "a floating point type required.");
// used to obtain a seed for the random number engine
std::random_device rd;
// standard mersenne_twister_engine seeded with rd()
std::seed_seq seed_seq {rd(), rd(), rd(), rd(), rd(), rd()};
static std::mt19937 gen {seed_seq};
std::uniform_real_distribution<T> dis(min, max);
return dis(gen);
}
template <typename T = int>
const
T
getRandomINT(const T min, const T max) noexcept;
template <typename T>
const
T
getRandomINT(const T min, const T max) noexcept
{
static_assert(std::is_integral<T>::value != 0, "an integral type required.");
// will be used to obtain a seed for the random number engine
std::random_device rd;
// standard mersenne_twister_engine seeded with rd()
std::seed_seq seed_seq {rd(), rd(), rd(), rd(), rd(), rd()};
static std::mt19937 gen {seed_seq};
std::uniform_int_distribution<T> dis(min, max);
return dis(gen);
}
} // namespace utilities