-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-random.cc
159 lines (139 loc) · 3.75 KB
/
01-random.cc
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Program
// Random numbers: Generate random numbers using different
// distribution (int + real).
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o 01-random 01-random.cc
//
// Execution
// ./01-random
//
#include <iostream>
#include <random>
#include <algorithm>
//
// Show numbers onto console
//
void show_numbers(const std::vector<int> &numbers) {
for (int i = 0; i < 10; ++i) {
std::cout << numbers[i] << " ";
}
std::cout << '\n';
}
//
// Entry function
//
int main() {
{
std::cout << "--- uniform_int_distribution ---" << '\n';
std::default_random_engine dre {};
std::uniform_int_distribution<int> uid(10, 20);
for (int i = 0; i < 10; ++i) {
std::cout << uid(dre) << " ";
}
std::cout << '\n';
}
{
std::cout << "--- uniform_int_distribution ---" << '\n';
std::default_random_engine dre {};
std::uniform_int_distribution<char> uid('a', 'z');
for (int i = 0; i < 10; ++i) {
std::cout << uid(dre) << " ";
}
std::cout << '\n';
}
{
std::cout << "--- std::shuffle @ uniform_int_distribution ---" << '\n';
std::vector<int> numbers {};
std::default_random_engine dre {};
std::uniform_int_distribution<int> uid(10, 20);
// Generate numbers
for (int i = 0; i < 10; ++i) {
numbers.push_back(uid(dre));
}
show_numbers(numbers);
// Shuffle the above generate numbers
std::shuffle(std::begin(numbers), std::end(numbers), dre);
show_numbers(numbers);
// Shuffle the above generate numbers
std::shuffle(std::begin(numbers), std::end(numbers), dre);
show_numbers(numbers);
// Shuffle the above generate numbers
std::shuffle(std::begin(numbers), std::end(numbers), dre);
show_numbers(numbers);
}
{
std::cout << "--- uniform_real_distribution ---" << '\n';
std::default_random_engine dre {};
std::uniform_real_distribution<double> urd(10.0, 20.0);
for (int i = 0; i < 10; ++i) {
std::cout << urd(dre) << " ";
}
std::cout << '\n';
}
{
std::cout << "--- discrete_distribution ---" << '\n';
std::default_random_engine dre {};
std::discrete_distribution<int> dd(10, 20);
for (int i = 0; i < 10; ++i) {
std::cout << dd(dre) << " ";
}
std::cout << '\n';
}
{
std::cout << "--- gamma_distribution ---" << '\n';
std::default_random_engine dre {};
std::gamma_distribution<double> gd(10.0, 20.0);
for (int i = 0; i < 10; ++i) {
std::cout << gd(dre) << " ";
}
std::cout << '\n';
}
{
std::cout << "--- binomial_distribution ---" << '\n';
std::default_random_engine dre {};
std::binomial_distribution<int> bd(10, 20);
for (int i = 0; i < 10; ++i) {
std::cout << bd(dre) << " ";
}
std::cout << '\n';
}
{
std::cout << "--- geometric_distribution ---" << '\n';
std::default_random_engine dre {};
std::geometric_distribution<int> gd(0.3);
for (int i = 0; i < 10; ++i) {
std::cout << gd(dre) << " ";
}
std::cout << '\n';
}
{
std::cout << "--- negative_binomial_distribution ---" << '\n';
std::default_random_engine dre {};
std::negative_binomial_distribution<int> nbd(10, 20);
for (int i = 0; i < 10; ++i) {
std::cout << nbd(dre) << " ";
}
std::cout << '\n';
}
{
std::cout << "--- poisson_distribution ---" << '\n';
std::default_random_engine dre {};
std::poisson_distribution<int> pd(2.0);
for (int i = 0; i < 10; ++i) {
std::cout << pd(dre) << " ";
}
std::cout << '\n';
}
{
std::cout << "--- exponential_distribution ---" << '\n';
std::default_random_engine dre {};
std::exponential_distribution<double> pd(2.0);
for (int i = 0; i < 10; ++i) {
std::cout << pd(dre) << " ";
}
std::cout << '\n';
}
return 0;
}