How does a rand() work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How does a rand() work?

how is a compiler able to set a value on ints own?? (i thought they where just a intermediator to os and machine)

22nd May 2017, 11:26 AM
U L Knw Me soon😉😋
U L Knw Me soon😉😋 - avatar
1 Answer
+ 1
it depends on its seed. if you dont change the seed it will always return the same value. thats why there is the srand function, that takes the seed as argument, which is an unsigned. there is also a function time which can return a time_t, the current time of the system when passed NULL as parameter, which is the same as an unsigned. srand(time(NULL)) this line sets as seed for rand the current time of the system, which will be different each time this gets executed. How is a pseudo-random number generated from a seed? following some simples rules, i guess, you can look it up on google.
22nd May 2017, 4:33 PM
Testing002