Normal(Gaussian) Distribution Function in C++

前端 未结 3 2063
遥遥无期
遥遥无期 2021-01-28 21:00

I need to know a way to have the Gaussian Distribution of 50 numbers. I know of the Boost library, which generates random numbers. In my case, I don\'t need random numbers; I ne

3条回答
  •  我在风中等你
    2021-01-28 21:50

    As of C++11 there is a normal (gaussian) distribution available in the standard library:

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

    The mean value and standard deviation are passed as arguments when creating it. The link above provides a good example:

    // normal_distribution
    #include 
    #include 
    
    int main()
    {
      const int nrolls=10000;  // number of experiments
      const int nstars=100;    // maximum number of stars to distribute
    
      std::default_random_engine generator;
      std::normal_distribution distribution(5.0,2.0);
    
      int p[10]={};
    
      for (int i=0; i=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;
    }
    

提交回复
热议问题