Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make OSX's rand() fail the spectral test?
    text
    copied!<p>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" <code>rand()</code> that comes with OSX (quoth the manpage).</p> <p>I wrote a simple program to test my understanding of the spectral test:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main() { int i; int prev = rand(); int new; for (i=0; i&lt;100000; i++) { new = rand(); printf("%d %d\n", prev, new); prev = new; } return 0; } </code></pre> <p>But when I plot the resulting scatterplot, here is what I get:</p> <p><img src="https://i.stack.imgur.com/Xdv5N.png" alt="Spectral test of OSX&#39;s rand()"></p> <p>I would have expected something showing more structure, like what one finds <a href="http://en.wikipedia.org/wiki/Spectral_test" rel="nofollow noreferrer">on Wikipedia</a>. Am I doing something wrong here? Should I plot in more dimensions?</p> <p><strong>UPDATE</strong></p> <p>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:</p> <p><img src="https://i.stack.imgur.com/mwXMb.png" alt="Spectral test of OSX&#39;s rand() limited to numbers smaller than 1e7"></p> <p>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 <code>rand()</code>. When I <code>sort -n</code> the data this is (a sample of) what I see:</p> <pre><code>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 </code></pre> <p>In other words, the points lie in lines that are almost, but not quite, vertical.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload