0

How come srand() produces same sequence using the same interger? Shouldn't srand change the sequence as true random?

9th Apr 2016, 9:56 PM
Michael A. Valverde
Michael A. Valverde - avatar
3 odpowiedzi
0
use srand( time( NULL ) );
5th Jul 2016, 7:51 AM
Mihai Dancaescu
Mihai Dancaescu - avatar
0
Also, time(0) returns the time passed from 1 jan 1970 until now on milliseconds.
5th Jul 2016, 7:53 AM
Mihai Dancaescu
Mihai Dancaescu - avatar
- 2
Imagine a simple math function: y = x, if I enter x=5, then y=5. No matter how many times I enter 5, I will always receive 5. rand() is no different from this principle, except it's more complex, and outputs a number that has seemingly no relation to thr input. Still, no matter what, if I give the same number, I'll receive the same sequence, just as if I enter 5 in the last function, I'll always receive 5. now whenever you enter time(0), it outputs the current time, which is hardly the same at any time. So it gives you some randomness into the program, but the rand() function by itself is not random. Hope this helps.
7th May 2016, 4:51 PM
Alejandro Aristizabal
Alejandro Aristizabal - avatar