0
Testing if rand is really random
What would be a good algorithm to show whether a list of values (eg returned by rand() ) are random?
5 Respuestas
0
yes sure Vladislav. but it does not answer the initial question. So how could we write a program that proves or disproves that the list of numbers returned by rand is likely to be random or not?
0
Remove srand() to prove that at each program launching it generates the same sequence
And the source of rand
https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=stdlib/rand_r.c;hb=HEAD
0
sure... but lets say we keep srand with a reasonable seed. What sort of program would show that the list of values returned by rand are random and not biased in some way?
0
Generated values are random for us and we can see no logical sequence.
The algorithm uses overflow so.. Yes, it's random
Example is in the 1st answer
- 1
rand() is not random.
It has seed - the first value, and complex algorithm that produce new value using previous.
Some examples
//main
srand(time(0));
for(int i = 0; i < 1000000; ++i)
cout << rand();
srand initialize seed with current time, so the generated sequence is "random"
but if you remove srand(), rand() generates always the same list of numbers.