+ 4
Seed rand
What is seed in random number and what does stand and rand do?
1 Odpowiedź
+ 7
rand() is a pseudo random function, which means you don’t get the perfectly random number, because computer has pre-defined behavior by its definition. This function simply executes the algorithm and creates a sequence of numbers and gives you one of them one by one every time you call it. This sequence depends on the initial number. Setting this initial number is called “seed” (srand stands for seed random). Basically, it means if you know the initial seed number, you always can predict the next random number. Try to use the rand() function without srand() and run the code multiple times, and you’ll see that you always get the same number (or the same sequence of numbers).
So, to get numbers much closer to random, you need to use srand().
The common use of srand() is:
srand(time(NULL));
Which sets the initial seed value based on time and makes your numbers as randomized as possible.