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

Author Topic: Rand() not working?  (Read 1467 times)

0 Members and 1 Guest are viewing this topic.

zephiro309

  • Newbie
  • *
  • Posts: 2
    • View Profile
Rand() not working?
« 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.
« Last Edit: December 08, 2012, 11:37:25 pm by zephiro309 »

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Rand() not working?
« Reply #1 on: December 09, 2012, 03:43:52 am »
srand() should be called one time in your program.

zephiro309

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Rand() not working?
« Reply #2 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.