Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Random values and probability distribution
//random number generators and distributions #include <iostream> #include <random> using namespace std; void UniformIntDist(){ const int nRolls = 100000; // number of experiments const int nStars = 95; // maximum number of stars to distribute default_random_engine generator; uniform_int_distribution<int>distribution(0, 9); int p[10] = {}; for(int i=0; i<nRolls; ++i){ int number = distribution(generator); ++p[number]; } cout << "uniform_int_distribution<int>distribution(0, 9):\n\n"; for(int i=0; i<10; ++i){ cout << i << ":" << string(p[i]*nStars/nRolls, '*') << endl; } }; void NormDist(){ const int nRolls = 100000; // number of experiments const int nStars = 100; // max numebr of stars const int classInt = 10; // class interval default_random_engine rvGenerator; normal_distribution<double>NDist(5.0, 2.0); int p[10] = {}; for(int i = 0; i < nRolls; i++){ double number = NDist(rvGenerator); if((number >= 0) && (number < 10)) ++p[int(number)]; } cout << "Normal Distribution: NDist(5.0, 2.0)\n\n"; for(int i=0; i<classInt; i++){ cout << i << " - " << (i+1) << "\t"; cout << string(p[i]*nStars/nRolls, '*') << endl; } }; void UniformRealDist(){ const int nRolls = 100000; // number of experiments const int nStars = 95; // max number of stars const int nIntervals = 10; // number of intervals default_random_engine generator; uniform_real_distribution<double>distribution(0.0, 1.0); int p[nIntervals] = {0}; for(int i=0; i<nRolls; ++i){ double number = distribution(generator); ++p[int(number*nIntervals)]; } cout << "uniform_real_distribution<double>distribution(0.0, 1.0)" << endl; cout.setf(ios::fixed); cout.precision(1); cout << endl; for(int i=0; i<nIntervals; ++i){ cout << float(i)/nIntervals << " - " << float(i+1)/nIntervals << " : "; cout << string(p[i]*nStars/nRolls, '*') << endl; } }; void inspect(){ default_random_engine gen; uniform_real_distribution<double>d1(0.0, 100.0); uniform_real_distribution<double>d2(d1.param()); cout << "Two independent random values:\n\n"; cout << "Random value 1 = " << d1(gen) << endl; cout << "Random value 2 = " << d2(gen) << endl; }; int main() { //UniformIntDist(); //cout << endl; //UniformRealDist(); // cout << endl; //inspect(); // cout << endl; NormDist(); return 0; }
run
|
edit
|
history
|
help
0
Access namespace by unqualified manner
Mock Interview Question
Forgetting to check end
Find in vector vs unordered_map
Throttle Example using a circular queue (Push all but 2 less than maxSize; then pop all but 2 of current size)
EBO
6 14 15 17 21 33 29
MY FIRST OBJECT ORIENTED PROGRAM
Result of not joining thread in main()
Exploring stringstreams