SFML community forums

Help => General => Topic started by: AdrianHEY on April 08, 2021, 11:19:09 am

Title: Every time I get a random number I get the same one.
Post by: AdrianHEY on April 08, 2021, 11:19:09 am
I am trying to spawn enemies on a random point in the windwo and every timpe it sapwns the enemy in the same position.

This is the code I used for the random spawn:

enemy.setPosition(1 + (rand() % 1920), 1 + (rand() % 1080));

Title: Re: Every time I get a random number I get the same one.
Post by: eXpl0it3r on April 08, 2021, 02:09:06 pm
For general C++ questions, please consider using StackOverflow or similar. :)

With C++11 and newer you should really be using the <random> header, see the "rand() Considered Harmful (https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful)" talk.

As for your code, if you don't seed your randomizer function with a different seed, you'll get predictably the same number every run.