forked from phpduke/Algorithms-Notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_distributions.cpp
More file actions
49 lines (40 loc) · 1.52 KB
/
random_distributions.cpp
File metadata and controls
49 lines (40 loc) · 1.52 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
//C++ 11 onwards
//Historgram Playground commented at bottom
#include <random>
#include <iostream>
int main()
{
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
//Various range distributions
std::uniform_int_distribution<> dis(1, 6);
std::uniform_real_distribution<> dis(1.0, 2.0);
std::geometric_distribution<> d; // same as std::negative_binomial_distribution<> d(1, 0.5);
// if particles decay once per second on average,
// how much time, in seconds, until the next one?
std::exponential_distribution<> d(1); //This is the continuous counterpart of geometric_distribution
// values near the mean are the most likely
// standard deviation affects the dispersion of generated values from the mean
std::normal_distribution<> d{5,2}; //5 is mean, dispersion from mean is at range +/-2
for (int n=0; n<10; ++n)
//Use dis to transform the random unsigned int generated by gen into an int in [1, 6]
std::cout << dis(gen) << ' ';
std::cout << '\n';
}
/*
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
// perform 4 trials, each succeeds 1 in 2 times
std::binomial_distribution<> d(4, 0.5);
std::map<int, int> hist;
for (int n = 0; n < 10000; ++n) {
++hist[d(gen)];
}
for (auto p : hist) {
std::cout << p.first << ' '
<< std::string(p.second/100, '*') << '\n';
}
}
*/