jeudi 19 mars 2015

Generate super random number in c++

C++11 Introduced the class that allows for generating very random numbers, it also creates an even distribution of random numbers. There is also implementation to generate a seed (a number used to make the Random Number Generator more random).


I am trying to make a function that generates a random number between min and max but I am having trouble. The function only generates the seeds and the random number once. When I call the function in other words it will keep giving me the same number.


Below is the code, I try to generate a bunch of seeds, pick one of them randomly, use that seed for the RNG and finaly produce a random number.



int Utils::GenerateSuperRandomNum(int min, int max)
{
//Seed a the RNG
int randNum;
int randIndex;
seed_seq seq{ 1, 2, 3, 4, 5 };
vector<int> seeds(5 * max);
uniform_int_distribution<int> rngDistribution(min, max); //Generates number in the range min to max.

//Generate our seed numbers.
seq.generate(seeds.begin(), seeds.end());

//Generate random index bewteen 0 and size - 1.
srand(seeds.at(0));
randIndex = rand() % seeds.size();

//Seed the RNG with a random seed from our vector.
mt19937 rngGenerator(seeds.at(randIndex));

//Get a random number.
randNum = rngDistribution(rngGenerator);

return randNum;
}

Aucun commentaire:

Enregistrer un commentaire