Skip to content

Commit 5c94659

Browse files
committed
random gen update
1 parent 979c8aa commit 5c94659

File tree

3 files changed

+222
-172
lines changed

3 files changed

+222
-172
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ target_link_libraries(default-initialized)
410410
add_executable(scope_resolution_operator src/scope_resolution_operator.cpp)
411411
target_link_libraries(scope_resolution_operator)
412412

413+
add_executable(random_number_generation src/random_number_generation.cpp)
414+
target_link_libraries(random_number_generation)
415+
416+
413417

414418
#set(CMAKE_CXX_FLAGS "-Wall -Wextra")
415419
#set(CMAKE_CXX_FLAGS_DEBUG "-g")

docs/random_number.md

Lines changed: 5 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Pseudo-random number generation
22

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).
44
`RAND_MAX` (macro constant) is the maximum possible value generated by `std::rand`
55

66
`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;
2020
```
2121
2222
23-
# Ranndom Generator From Specific Distribution
23+
# Random Generator From Specific Distribution
2424
2525
```cpp
26-
27-
#include <random>
28-
#include <map>
29-
30-
3126
int rangeFrom=0;
32-
int rangeTo=1
27+
int rangeTo=1;
3328
3429
std::random_device rand_dev;
3530
std::mt19937 generator(rand_dev());
@@ -68,142 +63,6 @@ weibull_distribution;
6863

6964
# Histogram of Distribution
7065

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-
20766
```
20867
------------------------bernoulli distribution------------------------
20968
0 *******************************************************************************************************************************************************************************************
@@ -216,13 +75,6 @@ output is:
21675
4 ****
21776
5 *
21877
6
219-
7
220-
8
221-
9
222-
10
223-
11
224-
12
225-
13
22678
14
22779
------------------------gamma distribution------------------------
22880
0 *******************************************************
@@ -237,22 +89,6 @@ output is:
23789
9 *
23890
10
23991
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
25692
------------------------normal distribution------------------------
25793
-10
25894
-9
@@ -300,11 +136,8 @@ output is:
300136
11
301137
12
302138
13
303-
14
304-
15
305-
16
306-
17
307139
308-
```
309140
141+
```
142+
[code](../src/random_number_generation.cpp)
310143

0 commit comments

Comments
 (0)