I am trying to write a simple program. I heard in C++ that there are existing functions that are better than rand()
(to make random numbers). I am talking about classes from #include <random>
. My program goal is to make random numbers.
This is my code:
int randomSpawn()
{
unsigned seed = time(NULL);
std::default_random_engine rng(seed);
std::uniform_int_distribution<int> d(1, LENGHT);
srand(time(NULL));
std::cout << d(rng) << std::endl;
std::cout << rand() % LENGHT << std::endl;
return rand() % LENGHT;
}
The problem is that the first value is always the same - 17
. LENGTH
is equal to 30.
So, the oldest version making random number is working - because it displays a random number, but the new version doesn't work because it always displays 17.
I tried to move the seed definition to another part of code, but it didn't help.
Aucun commentaire:
Enregistrer un commentaire