Randomly generated gaussion distribution array

Hi guys,

I am trying to create an array which inludes random gaussion distribution elements with mean = 0, standard deviation = 1 and size= 20. The corresponding python line is as follows:

np.random.normal(0, 1, 20)

I cannot do it in c++. Can you help pls? Thanks in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <random>
#include <ctime>
using namespace std;

mt19937 gen( time( 0 ) );


vector<double> getNormal( double mean, double sd, int N )
{
   normal_distribution<double> dist( mean, sd );
   vector<double> V( N );
   for ( double &e : V ) e = dist( gen );
   return V;
}


int main()
{
   vector<double> X = getNormal( 0, 1, 20 );
   for ( double e : X ) cout << e << ' ';
}

There are several distributions available in c++.

See http://www.cplusplus.com/reference/random/ for details.
Thanks a lot guys. You save me a lot of trouble.
Topic archived. No new replies allowed.