• Advertisement
Sign in to follow this  

intresting glitch

This topic is 4261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I have an intresting problem. In main() i declare two variables.
placex = rand () % 30;
placey = rand() % 10;

As you can see, each variable is set to be random. However, every time i recompile and run the program, the numbers are the same!!What is going on? -Thanks

Share this post


Link to post
Share on other sites
Advertisement
The rand() function isn't really random, you must seed it yourself.. This is done by calling srand() with a number:

srand(10);

for example.. Although, it would be better to seed with a variable value, for example time:

srand(time(NULL));

Share this post


Link to post
Share on other sites
For purposes of testing and simulation it is useful to have a repeatable set of pseudo-random numbers. Therefore, many pseudo-random number generators produce consistant results based on the value you use to seed it.

Share this post


Link to post
Share on other sites
u must create random seed by calling srand(i);
i:e.g. Current date/time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement