0
Why is it random?
Why is it random if it is not random? I mean it always outputs the same thing, so it is not random ??????
4 ответов
+ 3
You forgot to initialize the seed with srand(). Using the current time is a way to have a different seed at each execution.
srand(time(0));
for (int i = 0; i < 10; i++) {
cout << rand();
}
A pseudo-random number generator will give you the same serie of numbers for a given seed. This is very useful for simulations, as you want to be able to run a given simulation with exactly the same outcome.
A pseudo-random number generator doesn't generate random numbers per se, as computers are deterministic by nature, but the serie of numbers generated has properties that make them look like random numbers. This is usually good enough for games and simulations.
+ 2
time(0) gives you the current time (or more precisely the number of seconds since January 1, 1970).
0
thank you very much for your post. it was very helpful
0
and what is number 0 in srand()?