This code
#include <stdio.h> #include <stdlib.h> #include <time.h> int main () { printf ("First number: %d\n", rand() % 100); srand ( time(NULL) ); printf ("Random number: %d\n", rand() % 100); srand ( 1 ); printf ("Again the first number: %d\n", rand() %100); return 0; }
has the following output:
First number: 41 Random number: 13 Again the first number: 41
There is also the following rule:
Two different initializations with the same seed instruct the pseudo-random generator to generate the same sequence of results for subsequent calls to rand in both cases.
I understand the words, but I just do not understand the method itself. Why is he back again? Is it random or should it return the same result in each case according to this code?
c ++ random
dato datuashvili
source share