감동, 마음이 움직이는 것

[c++] random variable form Gaussian dist. 본문

Tips (Utility, Computer Language, and etc.)

[c++] random variable form Gaussian dist.

Struggler J. 2018. 11. 10. 00:23

http://www.cplusplus.com/reference/random/normal_distribution/



#include <iostream>

#include <string>

#include <random>

using namespace std;


int main() {

int nrolls = 10000; //the number of sampling trial

random_device rd;    //making random seed for generator

default_random_engine generator( rd() ); //making random generator using the random seed

normal_distribution<double> gaussian(5., 2.); //Gaussian distribution with mean 5 deviation 2


int p[10] = {};

double temp;

for (int i=0; i<nrolls; i++){

cout << gaussian(generator) << endl; //generating random number from the gaussian distribution

if (temp>0. && temp<10.) p[int(temp)]++;

}


//print the results

for(int i=0; i<10; i++){

cout << i << "-" << i+1 << ":";

cout << string(p[i]*nstars/nrolls,'*') << endl;

}


return 0;

}

You need -std=c++11 option to compile the code.