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

Author Topic: Random Position Problem  (Read 3480 times)

0 Members and 1 Guest are viewing this topic.

sfmlRocks

  • Newbie
  • *
  • Posts: 2
    • View Profile
Random Position Problem
« on: May 03, 2021, 06:24:28 pm »
Hi. I'm workin on space shooter. Trying to spawn enemy ships top of screen with random X position. If enemy ship not explodes and goes outside the window, I want to random it again(the x position) and position it outside (top, like y = -100.0f) the window. First random is good, lets say 155, the next randoms are 165, 175,185... etc.

i have enemy class and Init function
its basically;
void Init(){
    srand(time(NULL));
    enemyShip.setPosition(rand()%350 + 50, -100.0f);
}

and i check enemy's y position in everyframe. if y pos > 750 -> init the same ship with random X and -100.0f y position.

screen width:400, height:700, enemyship size x,y = 50.0f

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Random Position Problem
« Reply #1 on: May 03, 2021, 08:48:01 pm »
I didn't really get what the problem is, but doesn't seems to be related to SFML... in the small code you posted, I didn't find anything wrong (altough I wouldn't use rand(), but thats a personal choice)
Visit my game site (and hopefully help funding it? )
Website | IndieDB

kojack

  • Sr. Member
  • ****
  • Posts: 310
  • C++/C# game dev teacher.
    • View Profile
Re: Random Position Problem
« Reply #2 on: May 04, 2021, 08:01:17 am »
Is that Init function called every time a ship is reset to the top?
Generally srand should only be called once in the entire program (at the start).

Especially if using time() to seed it. time() works in seconds, if you srand before every rand then you'll get new random values only once per second. For example, if you reset 10 ships within a second, they will all get the same X position because the srand is resetting the random sequence back to the same starting point until the result of time() changes.

The C/C++ rand is pretty crap for repeating patterns anyway (and only returns a 15 bit value).

I generally use a 128bit Xor Shift random number generator. http://en.wikipedia.org/wiki/Xorshift
But C++11 brought in a new random number system: https://www.cplusplus.com/reference/random

sfmlRocks

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Random Position Problem
« Reply #3 on: May 04, 2021, 09:04:52 pm »
@kojack
Yes, if it gets hit, or just go through the bottom of the window, i call init and reuse the same ship. I've tried default random engine etc. but nothing changed. So i create an array with X positions (10, 20, 30...), and get one of them randomly. Just worked perfectly fine. I really have no idea about c++ random. I'm actually use unity and c# handles perfect randoms. Thanks for the links btw, i'll take a look.

@Stauricus
Yeap sorry actually it was about C++, i've managed the get random positions btw (still with rand() :/ )

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Random Position Problem
« Reply #4 on: May 05, 2021, 04:07:44 pm »
Regarding rand() vs <random> I highly recommend to watch: rand() Considered Harmful

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything