The purpose of rand() is to get a random (well, seemingly random) value.
srand() sets the starting point for the repeatable series of random values. Each time you call srand(), if you feed it the same seed, it will reset the series, and instead of getting seemingly random values, you get the same series you got last time i.e. not random anymore.
Since most new users want random values, if they call it twice or more during the course of their program, they are probably doing it accidentally, not understanding what it does, and not understanding that it makes their values less random.
It's not at all bad to call srand() multiple times. It's not at all bad to use srand() to intentionally get the same repeatable series of numbers. But if that's not what a new programmer is wanting or expecting, then they come asking why it's not random. The easiest explanation is, if you want it random, seed it with time() and only once at startup.
If it's not seeded with time() (which returns the number of seconds since January 1st, 1970), I think it defaults to a seed of 0, which means each time the user starts the program, they get the same series of "random" numbers, and think rand() is broken.
- "How come each time I run my game, the random numbers are the same series/pattern?!" -> You didn't seed it, or else seeded it with the same value each time the program runs.
- "How come each time I run my game, every number is completely identical, even in the same execution?!" -> You call srand() before each usage of rand(), and reset the sequence, and you seeded it with an unchanging value.
- "How when I call rand() multiple times in a row, the numbers are identical, but in other places in my code, a different group of rand() calls has a different identical number?" -> You call srand(time()) before each call to rand(), but a full second hasn't passed between each call to ssrand() in that particular block of code (so you are resetting the sequence, and in the consecutive calls to srand(), time() is feeding it the same value since a full second hasn't passed and time() returns the number of seconds since Jan 1st, 1970).
It's perfectly fine to call srand() more than once. Not in the least harmful. But new programmers who don't understand what srand() does, and didn't read the documentation, they are using srand() incorrectly, and almost always (>99%) are only really wanting it to be seeded with time() and called once at startup. They can learn the rule first "Don't call srand() more than once, and only call it with time()", and learn the exception to the rule later, "unless you intentionally want to repeat the same sequence of numbers, which is useful for things like procedural generation". Their main focus at their current level of code should be understanding code-flow, not the scientific theory of random number generators.
It's a perfectly reasonable and helpful answer, though I like to give the exception with the rule, "Don't call srand() more than once, and only call it with time(), unless you intentionally want to repeat the same sequence of numbers, which is useful for things like procedural generation".
A perfectly unreasonable and stupid answer is the people who reply to a 12 year old programming newbie with, "Don't use rand(), it's inaccurate after the 2 billionth number is generated, and certain values are more likely to come up with an 0.003% higher probability than others. Instead, forget about completing your stupid little project, and go look up real fake psuedonumericallyperfectrandom number generators that college students study in compute science class. It'll waste the next week of your life, and you won't understand it anyway (since you are still learning the basics of programming), but at least you'll think I am some awesome l33t c0d1ng haxor."
Edited by Servant of the Lord, 27 December 2012 - 03:24 PM.