Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Every time I get a random number I get the same one.  (Read 2996 times)

0 Members and 1 Guest are viewing this topic.

AdrianHEY

  • Newbie
  • *
  • Posts: 14
    • View Profile
Every time I get a random number I get the same one.
« 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));


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Every time I get a random number I get the same one.
« Reply #1 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" 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything