Why is it random? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
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 ??????

24th Oct 2016, 1:19 PM
Péter Subecz
Péter Subecz - avatar
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.
25th Oct 2016, 11:42 AM
Zen
Zen - avatar
+ 2
time(0) gives you the current time (or more precisely the number of seconds since January 1, 1970).
26th Oct 2016, 9:08 PM
Zen
Zen - avatar
0
thank you very much for your post. it was very helpful
26th Oct 2016, 7:55 PM
Péter Subecz
Péter Subecz - avatar
0
and what is number 0 in srand()?
26th Oct 2016, 7:58 PM
Péter Subecz
Péter Subecz - avatar