Seed rand | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Seed rand

What is seed in random number and what does stand and rand do?

25th Apr 2018, 3:59 PM
Black Widow
Black Widow - avatar
1 Answer
+ 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.
25th Apr 2018, 7:49 PM
Ali Zhussupov
Ali Zhussupov - avatar