mercredi 26 juillet 2017

Confused in 2D random array number generation using c++

I was using c++ to generate two 2D random array. However the results confused me when codeing a little differently in for loop. Can anyone know the reason? I appreciate your help. I used gcc 4.8.4 with -std=c++11 option to compile. Below is the code,

#include <iostream>
#include <string>
#include <random>
#include <iomanip>
#include <new>
#include <math.h>

int main ()
{

  std::default_random_engine generator,generator2;
  std::normal_distribution<double> distribution;
  double **peta, **pxi;
  const int Nmodes =200;
  const double PI=3.1415926;

  peta = new double*[3];
  pxi = new double*[3];
  for (int i=0; i<3; ++i)
    {
      peta[i] = new double[Nmodes];
      pxi[i] = new double[Nmodes];
    }

  distribution.param(std::normal_distribution<double>(0.0,1.0).param());

  for (int i=0; i<3; ++i)
    {
      for (int j=0; j<Nmodes; ++j)
        {
          peta[i][j]=distribution(generator);
          // pxi[i][j] =distribution(generator2);  //1st case
        }
      for (int j=0; j<Nmodes; ++j)
        {
           pxi[i][j] =distribution(generator2); //2nd case
        }
    }
  std::cout<<std::setprecision(15)<<"peta "<<peta[0][20]<<'+'<<peta[1][20]<<'+'<<peta[2][20]<<'+'<<peta[2][30]<<std::endl;
  std::cout<<"pxi "<<pxi[0][20]<<"+"<<pxi[1][20]<<'+'<<pxi[2][20]<<'+'<<pxi[2][30]<<std::endl;



    for (int i=0; i<3; ++i)    
{
      delete [] peta[i];
      delete [] pxi[i];
    }
  delete [] peta;
  delete [] pxi;

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire