감동, 마음이 움직이는 것

[C++] gaussian sampling 본문

Tips (Utility, Computer Language, and etc.)

[C++] gaussian sampling

Struggler J. 2017. 5. 18. 05:34

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

http://stackoverflow.com/questions/19944111/creating-a-gaussian-random-generator-with-a-mean-and-standard-deviation

http://stackoverflow.com/questions/2325472/generate-random-numbers-following-a-normal-distribution-in-c-c

https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform


#include <random>

int main()
{
  std::default_random_engine generator;
  std::normal_distribution<double> distribution(5.0,2.0);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  std::cout << "normal_distribution (5.0,2.0):" << std::endl;

  for (int i=0; i<10; ++i) {
    std::cout << i << "-" << (i+1) << ": ";
    std::cout << std::string(p[i]*nstars/nrolls,'*') << std::endl;
  }

  return 0;
}

output:

normal_distribution (5.0,2.0):
0-1: *
1-2: ****
2-3: *********
3-4: ***************
4-5: ******************
5-6: *******************
6-7: ***************
7-8: ********
8-9: ****
9-10: *


[cf] using the cdf 

sampling from PDF.PDF