Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For a surreal-like decimal expansion, you need a random binary number. <em>Even</em> bits tell you whether to stop or continue, <em>odd</em> bits tell you whether to go right or left on the tree:</p> <pre><code>&gt; 0... =&gt; 0.0 [50%] Stop &gt; 100... =&gt; -0.5 [&lt;12.5%] Go, Left, Stop &gt; 110... =&gt; 0.5 [&lt;12.5%] Go, Right, Stop &gt; 11100... =&gt; 0.25 [&lt;3.125%] Go, Right, Go, Left, Stop &gt; 11110... =&gt; 0.75 [&lt;3.125%] Go, Right, Go, Right, Stop &gt; 1110100... =&gt; 0.125 &gt; 1110110... =&gt; 0.375 &gt; 1111100... =&gt; 0.625 &gt; 1111110... =&gt; 0.875 </code></pre> <p>One way to quickly generate a random binary number is by looking at the decimal digits in math.random() and replace 0-4 with '1' and 5-9 with '1':</p> <ul> <li><p>0.8430419054348022 becomes 1000001010001011 which becomes <code>-0.5</code> </p></li> <li><p>0.5513009827118367 becomes 1100001101001011 which becomes <code>0.25</code> etc</p></li> </ul> <p>Haven't done much lua programming, but in Javascript you can do:</p> <pre><code>Math.random().toString().substring(2).split("").map( function(digit) { return digit &gt;= "5" ? 1 : 0 } ); </code></pre> <p>or true binary expansion:</p> <pre><code>Math.random().toString(2).substring(2) </code></pre> <p>Not sure which is more genuinely "random" -- you'll need to test it.</p> <p>You <em>could</em> generate surreal numbers in this way, but most of the results will be decimals in the form a/2^b, with relatively few integers. On Day 3, only 2 integers are produced (-3 and 3) vs. 6 decimals, on Day 4 it is 2 vs. 14, and on Day n it is 2 vs (2^n-2).</p> <p>If you add two uniform random numbers from <code>math.random()</code>, you get a new distribution which has a "triangle" like distribution (linearly decreasing from the center). Adding 3 or more will get a more 'bell curve' like distribution centered around 0:</p> <pre><code>math.random() + math.random() + math.random() - 1.5 </code></pre> <p>Dividing by a random number will get a truly wild number:</p> <pre><code>A/(math.random()+1e-300) </code></pre> <p>This will return an results between A and (<em>theoretically</em>) A*1e+300, though my tests show that 50% of the time the results are between A and 2*A and about 75% of the time between A and 4*A.</p> <p>Putting them together, we get:</p> <pre><code>round(6*(math.random()+math.random()+math.random() - 1.5)/(math.random()+1e-300)) </code></pre> <p>This has over 70% of the number returned between -9 and 9 with a few big numbers popping up rarely.</p> <p>Note that the average and sum of this distribution will tend to diverge towards a large negative or positive number, because the more times you run it, the more likely it is for a small number in the denominator to cause the number to "blow up" to a large number such as 147,967 or -194,137.</p> <p>See <a href="https://gist.github.com/3957208" rel="nofollow">gist</a> for sample code.</p> <p><a href="http://about.me/joshuaweinstein" rel="nofollow">Josh</a></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