<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The Osmosian Order of Plain English Programmers Welcomes You]]></provider_name><provider_url><![CDATA[http://osmosianplainenglishprogramming.blog]]></provider_url><author_name><![CDATA[gerryrzeppa]]></author_name><author_url><![CDATA[https://osmosianplainenglishprogramming.blog/author/gerryrzeppa/]]></author_url><title><![CDATA[Random Numbers]]></title><type><![CDATA[link]]></type><html><![CDATA[<header class="entry-header">
<h1 class="entry-title"></h1>
</header>
<div class="entry-content">
<p>When a programmer asks for a a series of random numbers, he typically doesn’t want <em>truly </em>random numbers. What he generally wants is numbers that are evenly distributed over a given range, supplied in an unexpected sequence.</p>
<p>Plain English’s random number generator is a hybrid of English and Intel x86 machine code (for speed). It uses a “seed” that is initialized to the system’s tickcount when a program starts running. The seed can be set by the programmer to a specific value if he wants the same sequence of numbers to be generated with each run. This is the routine:</p>
<p><span style="color:#00ccff;">To pick a random number between a min number and a max number:</span><br />
<span style="color:#00ccff;"> Put the seed&#8217;s whereabouts into eax.</span><br />
<span style="color:#00ccff;"> Intel $8BC8. \ mov ecx,eax<span style="color:#000000;"> \ calculate zero based max</span></span><br />
<span style="color:#00ccff;"> Intel $8B8510000000.<span style="color:#000000;"> \ mov eax,[ebp+16]</span></span><br />
<span style="color:#00ccff;"> Intel $8B00. \ mov eax,[eax] <span style="color:#000000;">\ dereference</span></span><br />
<span style="color:#00ccff;"> Intel $8B9D0C000000. <span style="color:#000000;">\ mov ebx,[ebp+12]</span></span><br />
<span style="color:#00ccff;"> Intel $2B03. <span style="color:#000000;">\ sub eax,[ebx]</span></span><br />
<span style="color:#00ccff;"> Intel $40.<span style="color:#000000;"> \ inc eax \ adjust randseed</span></span><br />
<span style="color:#00ccff;"> Intel $691105840808.<span style="color:#000000;"> \ imul edx,[ecx],134775813</span></span><br />
<span style="color:#00ccff;"> Intel $42.<span style="color:#000000;"> \ inc edx</span></span><br />
<span style="color:#00ccff;"> Intel $8911.<span style="color:#000000;"> \ mov [ecx],edx</span></span><br />
<span style="color:#00ccff;"> Intel $F7E2.<span style="color:#000000;"> \ mul edx</span></span><br />
<span style="color:#00ccff;"> Intel $0313.<span style="color:#000000;"> \ add edx,[ebx] the min</span></span><br />
<span style="color:#00ccff;"> Intel $8B9D08000000.<span style="color:#000000;"> \ mov ebx,[ebp+08] </span></span><br />
<span style="color:#00ccff;"> Intel $8913.<span style="color:#000000;"> \ mov [ebx],edx</span></span><br />
<span style="color:#00ccff;"> Put the random number into the context&#8217;s number.</span></p>
<p>Everything following a backslash to the end of the line is a comment. Comments appear in a different color than &#8220;code&#8221; in our editor so they can be easily ignored. (The colors in this article are reversed.) The algorithm is essentially the same as the <em>linear congruential generator</em>that was used in Borland’s Turbo Pascal.</p>
<p>Now here is a program that illustrates exactly how uniform a distribution that routine generates:</p>
<p><span style="color:#00ccff;">To run:</span><br />
<span style="color:#00ccff;"> Start up.</span><br />
<span style="color:#00ccff;"> Clear the screen using the tan color.</span><br />
<span style="color:#00ccff;"> Make a box 2 inches by 2 inches.</span><br />
<span style="color:#00ccff;"> Center the box on the screen. Move the box left 5 inches.</span><br />
<span style="color:#00ccff;"> Fill and label the box with random spots stopping at 100.</span><br />
<span style="color:#00ccff;"> Move the box right 2-1/2 inches.</span><br />
<span style="color:#00ccff;"> Fill and label the box with random spots stopping at 1000.</span><br />
<span style="color:#00ccff;"> Move the box right 2-1/2 inches.</span><br />
<span style="color:#00ccff;"> Fill and label the box with random spots stopping at 10000.</span><br />
<span style="color:#00ccff;"> Move the box right 2-1/2 inches.</span><br />
<span style="color:#00ccff;"> Fill and label the box with random spots stopping at 100000.</span><br />
<span style="color:#00ccff;"> Move the box right 2-1/2 inches.</span><br />
<span style="color:#00ccff;"> Fill and label the box with random spots stopping at 1000000.</span><br />
<span style="color:#00ccff;"> Refresh the screen.</span><br />
<span style="color:#00ccff;"> Wait for the escape key.</span><br />
<span style="color:#00ccff;"> Shut down.</span></p>
<p><span style="color:#00ccff;">To fill and label a box with random spots stopping at a number:</span><br />
<span style="color:#00ccff;"> Privatize the number.</span><br />
<span style="color:#00ccff;"> Draw the box with the brown pen.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Pick a spot anywhere in the box.</span><br />
<span style="color:#00ccff;"> Draw the spot with the brown pen.</span><br />
<span style="color:#00ccff;"> If a counter is past the number, break.</span><br />
<span style="color:#00ccff;"> Repeat.</span><br />
<span style="color:#00ccff;"> Write the number under the box.</span><br />
<span style="color:#00ccff;"> Refresh the screen.</span></p>
<p><span style="color:#00ccff;">To pick a spot anywhere in a box:</span><br />
<span style="color:#00ccff;"> Pick the spot&#8217;s x between the box&#8217;s left and the box&#8217;s right.</span><br />
<span style="color:#00ccff;"> Pick the spot&#8217;s y between the box&#8217;s top and the box&#8217;s bottom.</span></p>
<p>And here is what we get when we run it:</p>
<p><img loading="lazy" class="alignnone size-large wp-image-325689" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/random-number-map-1-1024x819.jpg" alt="" width="665" height="532" /></p>
<p>You can see that the distribution is reasonably uniform in each case. Plain English uses a 96 pixel-per-inch resolution, so a 2-inch square contains 36,864 spots. It takes many more iterations than that to completely fill the box, however, because the random number generator sometimes returns a number more than once. It returns fewer duplicates, of course, when the range (the size of the box, in this case) is larger. Just 10,000,000 iterations, for example, was enough to almost completely fill my entire, 1,310,720-pixel screen.</p>
</div>
]]></html><thumbnail_url><![CDATA[https://i0.wp.com/cdncontribute.geeksforgeeks.org/wp-content/uploads/random-number-map-1-1024x819.jpg?fit=440%2C330&ssl=1]]></thumbnail_url><thumbnail_width><![CDATA[413]]></thumbnail_width><thumbnail_height><![CDATA[330]]></thumbnail_height></oembed>