1
1
# Pseudo-random number generation
2
2
3
- ` int std::rand(); ` : Returns a pseudo-random integral value between ` 0 ` and ` RAND_MAX ` (both numbers included).
3
+ ` int std::rand() ` : returns a pseudo-random integral value between ` 0 ` and ` RAND_MAX ` (both numbers included).
4
4
` RAND_MAX ` (macro constant) is the maximum possible value generated by ` std::rand `
5
5
6
6
` std::srand() ` seeds the ` rand() ` . If ` rand() ` is used before any calls to ` std::srand( unsigned seed) ` , ` rand() ` behaves as if it was seeded with ` std::srand(1) ` and each time ` rand() ` is seeded with ` std::srand() ` , it must produce the same sequence of values on successive calls. to generate random number between 0 and your upperbound just use ` rand() % upperbound ` .
@@ -20,16 +20,11 @@ std::cout<<(rand() % upperbound) <<std::endl;
20
20
```
21
21
22
22
23
- # Ranndom Generator From Specific Distribution
23
+ # Random Generator From Specific Distribution
24
24
25
25
```cpp
26
-
27
- #include <random>
28
- #include <map>
29
-
30
-
31
26
int rangeFrom=0;
32
- int rangeTo=1
27
+ int rangeTo=1;
33
28
34
29
std::random_device rand_dev;
35
30
std::mt19937 generator(rand_dev());
@@ -68,142 +63,6 @@ weibull_distribution;
68
63
69
64
# Histogram of Distribution
70
65
71
- ``` cpp
72
- #include < random>
73
- #include < map>
74
- #include < iomanip>
75
-
76
- template < typename T>
77
- void calculatePrintHistogram (T d, std::mt19937 gen, int number_of_samples = 1000000, int histogram_bin_size = 4000)
78
- {
79
- std::map<int, int> hist{};
80
-
81
-
82
- for (int n = 0; n < number_of_samples; ++n)
83
- {
84
- ++hist[std::round(d(gen))];
85
- }
86
- for (auto p : hist)
87
- {
88
- std::cout << std::setw(2) << p.first << ' ' << std::string(p.second / histogram_bin_size, '*') << '\n';
89
- }
90
-
91
- }
92
-
93
- int main()
94
- {
95
-
96
- enum Distributions
97
- {
98
- bernoulli_distribution,
99
- exponential_distribution,
100
- gamma_distribution,
101
- normal_distribution,
102
- poisson_distribution,
103
- uniform_real_distribution
104
- };
105
-
106
-
107
- int number_of_samples = 1000000;
108
- int histogram_bin_size = 2500;
109
-
110
-
111
- std::random_device rd{};
112
- std::mt19937 gen{ rd() };
113
-
114
- /*parameters for normal_distribution*/
115
- double mean = 5;
116
- double sigma = 3;
117
-
118
-
119
- /*parameters for uniform_real_distribution*/
120
- double rangeFrom = 0;
121
- double rangeTo = 20;
122
-
123
- /*parameters for bernoulli_distribution
124
- give "true" 1/4 of the time, give "false" 3/4 of the time*/
125
- double chance_of_wining = 0.25;
126
-
127
-
128
- /*parameters for poisson_distribution
129
- if an event occurs 4 times a minute on average
130
- how often is it that it occurs n times in one minute?*/
131
- int frequency_of_occurance_in_a_minute_on_average = 4;
132
-
133
-
134
- /* parameters for exponential_distribution
135
- if particles decay once per second on average,
136
- how much time, in seconds, until the next one?*/
137
- int decay_once_per_second_on_average = 1;
138
-
139
-
140
- /*
141
- A gamma distribution with alpha = 1, and beta = 2
142
- approximates an exponential distribution.
143
- */
144
- int alpha = 1;
145
- int beta = 2;
146
-
147
-
148
-
149
- for (int it = bernoulli_distribution; it != uniform_real_distribution; it++)
150
- {
151
- Distributions distribution = static_cast<Distributions>(it);
152
-
153
- switch (distribution)
154
- {
155
- case bernoulli_distribution:
156
- {
157
- std::bernoulli_distribution d(chance_of_wining);
158
- std::cout<<"------------------------bernoulli distribution------------------------" <<std::endl;
159
- calculatePrintHistogram(d, gen);
160
- }
161
- break;
162
- case exponential_distribution:
163
- {
164
- std::exponential_distribution<> d(decay_once_per_second_on_average);
165
- std::cout << "------------------------exponential distribution------------------------" << std::endl;
166
- calculatePrintHistogram(d, gen);
167
- }
168
- break;
169
- case gamma_distribution:
170
- {
171
- std::gamma_distribution<> d(alpha, beta);
172
- std::cout << "------------------------gamma distribution------------------------" << std::endl;
173
- calculatePrintHistogram(d, gen);
174
- }
175
- break;
176
- case normal_distribution:
177
- {
178
- std::normal_distribution<> d{ mean, sigma };
179
- std::cout << "------------------------normal distribution------------------------" << std::endl;
180
- calculatePrintHistogram(d, gen);
181
- }
182
- break;
183
-
184
- case poisson_distribution:
185
- {
186
- std::poisson_distribution<> d(frequency_of_occurance_in_a_minute_on_average);
187
- std::cout << "------------------------poisson distribution------------------------" << std::endl;
188
- calculatePrintHistogram(d, gen);
189
- }
190
- break;
191
-
192
- case uniform_real_distribution:
193
- {
194
- std::uniform_real_distribution<double> d(rangeFrom, rangeTo);
195
- std::cout << "------------------------uniform real distribution------------------------" << std::endl;
196
- calculatePrintHistogram(d, gen);
197
- }
198
- break;
199
-
200
- default:
201
- break;
202
- }
203
- }
204
- ```
205
- output is:
206
-
207
66
```
208
67
------------------------bernoulli distribution------------------------
209
68
0 *******************************************************************************************************************************************************************************************
@@ -216,13 +75,6 @@ output is:
216
75
4 ****
217
76
5 *
218
77
6
219
- 7
220
- 8
221
- 9
222
- 10
223
- 11
224
- 12
225
- 13
226
78
14
227
79
------------------------gamma distribution------------------------
228
80
0 *******************************************************
@@ -237,22 +89,6 @@ output is:
237
89
9 *
238
90
10
239
91
11
240
- 12
241
- 13
242
- 14
243
- 15
244
- 16
245
- 17
246
- 18
247
- 19
248
- 20
249
- 21
250
- 22
251
- 23
252
- 24
253
- 25
254
- 26
255
- 27
256
92
------------------------normal distribution------------------------
257
93
-10
258
94
-9
@@ -300,11 +136,8 @@ output is:
300
136
11
301
137
12
302
138
13
303
- 14
304
- 15
305
- 16
306
- 17
307
139
308
- ```
309
140
141
+ ```
142
+ [ code] ( ../src/random_number_generation.cpp )
310
143
0 commit comments