Public Group

# intresting glitch

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

## 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 on other sites
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 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 on other sites
u must create random seed by calling srand(i);
i:e.g. Current date/time.

fixed

• 9
• 13
• 41
• 15
• 13