For the purposes of a programming class I'm trying to illustrate the weaknesses of the random number generators that usually come with the standard C library, specifically the "bad random generator" rand()
that comes with OSX (quoth the manpage).
I wrote a simple program to test my understanding of the spectral test:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
int prev = rand();
int new;
for (i=0; i<100000; i++) {
new = rand();
printf("%d %d\n", prev, new);
prev = new;
}
return 0;
}
But when I plot the resulting scatterplot, here is what I get:
I would have expected something showing more structure, like what one finds on Wikipedia. Am I doing something wrong here? Should I plot in more dimensions?
UPDATE
Following pjs's suggestion I zoomed in on the part of the plot where the numbers are smaller than 1e7, and here is what I found:
I find exactly the same lines showed by pjs. They seem to be vertical, but this is impossible as it would imply that some values were "missed" by rand()
. When I sort -n
the data this is (a sample of) what I see:
571 9596797
572 9613604
575 9664025
578 9714446
580 9748060
581 9764867
584 9815288
586 9848902
587 9865709
590 9916130
592 9949744
127774 13971
127775 30778
127780 114813
127781 131620
127782 148427
127783 165234
127785 198848
127787 232462
127788 249269
In other words, the points lie in lines that are almost, but not quite, vertical.
rand()
like intensity of point!rand()
were a problem? What you're trying to show is that knowing the last n results gives you grounds to make some prediction about the next. On the wikipedia page I think the point being made is that any two outputs from that particular generator allows you to make a pretty good guess about the third. So in this case I think what your graph shows is that knowing one doesn't give you enough information to make a good guess at the next. So as Daniel says, probably you need to try plotting in higher dimensions.