-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRandom.cpp
More file actions
94 lines (74 loc) · 2.7 KB
/
Random.cpp
File metadata and controls
94 lines (74 loc) · 2.7 KB
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
92
93
94
// =====================================================================================
// Random.cpp // Random Numbers
// =====================================================================================
module modern_cpp:random;
namespace Random {
static void main_random_01()
{
// pseudo-random numbers engine
std::default_random_engine engine{};
// or
// std::default_random_engine engine{ static_cast<unsigned int> (time(0)) };
// random integer numbers from a given input range with uniform probabilty
std::uniform_int_distribution<int> distribution{ 0, 5 };
// need vector of length 6 zero-initialized
std::vector<int> numbers(6, 0);
// generate random numbers
for (int i = 0; i < 6 * 100000; i++) {
int random = distribution(engine);
numbers[random]++;
}
// print numbers
for (int n : numbers)
std::cout << n << " ";
std::cout << std::endl;
}
static void main_random_02()
{
// pseudo-random numbers engine
std::random_device engine{};
std::mt19937 generator(engine());
// random integer numbers from a given input range with uniform probabilty
std::uniform_int_distribution<int> distribution{ 0, 5 };
// need vector of length 6 zero-initialized
std::vector<int> numbers(6, 0);
// generate random numbers
for (int i = 0; i < 6 * 100000; i++) {
int random = distribution(engine);
numbers[random]++;
}
// print numbers
for (int n : numbers)
std::cout << n << " ";
std::cout << std::endl;
}
static void main_random_03()
{
static const std::size_t numCoins = 10;
// pseudo-random numbers engine
std::default_random_engine engine{};
// throw 10 coins, 0..10-time 'Kopf' / binomial probabilty
std::binomial_distribution<int> distribution{ numCoins };
// need vector of length 10 zero-initialized (10 coins)
std::vector<int> coins(numCoins + 1, 0);
// generate random numbers
for (int i = 0; i < 10 * 100000; i++) {
int random = distribution(engine);
coins[random]++;
}
// print numbers
for (int n : coins)
std::cout << n << " ";
std::cout << std::endl;
}
}
void main_random()
{
using namespace Random;
main_random_01();
main_random_02();
main_random_03();
}
// =====================================================================================
// End-of-File
// =====================================================================================