jeudi 26 mars 2020

Random numbers in a constructor of an object array

Firstly, sorry if it's a silly question, but I have recently learn how to deal with classes. My problem is that I've created a constructor which gives every data member a random number, with the following syntax:

#include <random>

...

//Constructor
cSystem::cSystem(void)
{
//Range of random numbers
double lower_bound = -100;
double upper_bound = 100;
std::uniform_real_distribution<double> unif(lower_bound,upper_bound);
std::default_random_engine re;

/*Initialization of data members*/
sMass=1.0014;
sPositionX= unif(re);
sPositionY= unif(re);
}

But then, I want to create an array of these "systems" in my main function

int main (void)
{
cSystem systems[numsist]; //numsist is a constant defined to be 1000

//Function that writes its characteristics in a file using a method that returns positions
getPositions(systems);

return 0;
}

But in the file, I just get

-73.6924    -8.26997
-73.6924    -8.26997
-73.6924    -8.26997

I thought that I was going through the constructor for every element of the array, giving it a different random value, but it seems like a single value is given at the beginning and then it is repeated throughout the vector. What can I do to get a constructor that initializes every element in a random way?

Note: I also get the same values everytime I run the program, I suppose that the seed does not change, why is that the case?




Aucun commentaire:

Enregistrer un commentaire