Random number from normal distribution in C++

前端 未结 2 1042
谎友^
谎友^ 2021-01-19 17:02

As a complete beginner to C++, I would like to generate a random number from a normal distribution.

With the following code (derived from this post), I am able to d

2条回答
  •  天命终不由人
    2021-01-19 17:59

    Use a seed to initialize your generator. Here I am using a time-based seed.

    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
        unsigned seed = chrono::system_clock::now().time_since_epoch().count();
        default_random_engine generator(seed);
        normal_distribution distribution(0.0, 1.0);
    
        cout << distribution(generator);
        return 0;
    }
    

提交回复
热议问题