SFML community forums

Help => General => Topic started by: zephiro309 on December 08, 2012, 11:33:34 pm

Title: Rand() not working?
Post by: zephiro309 on December 08, 2012, 11:33:34 pm
Im trying to relocate a sprite using rand(), but it doesn't seem to work

Considering the following excerpt:

std::cout << "Relocating goal..." << std::endl;
                        srand((unsigned)time(NULL));
                        stick_x = (rand() % (640 - 32)) + 1;
                        stick_y = (rand() % (360 - 32)) + 1;
                        std::cout << "Position change..." << std::endl;
                        stick_sprite.setPosition(stick_x, stick_y);

X only increments by a few bits (4 or 5) and Y keeps decrementing (by, like, 30). I have no idea why. Also, this happens upon collision. However, when there is a change in position in only occurs after a second, instead on instantly.
Title: Re: Rand() not working?
Post by: cire on December 09, 2012, 03:43:52 am
srand() should be called one time in your program.
Title: Re: Rand() not working?
Post by: zephiro309 on December 09, 2012, 12:53:36 pm
Thanks, that fixed it. It was sort of a stupid mistake too, but I went around this several times without being able to fix it.