+ 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)
1 Resposta
+ 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.