<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel=StyleSheet href="../ALL.CSS" type="text/css">
<link rel=StyleSheet href="COMPUTER.CSS" type="text/css">
<title>Ted's Computer World - Beware Basic's Non-Random Number Generator</title>
<style type="text/css">
 .code {font: 110% arial,sans-serif; color: #008; margin-left: 3em} 
</style>
</head>
<body>

<table class="topcolor" style="width:100%"><col span="2" style="width:50%">
  <tr>
    <td><img src="img/{Petey-Computer.gif" alt="Ted's Computer World"></td>
    <td class="name">BASIC&nbsp; Non-Random&nbsp; Numbers</td>     
  </tr>
</table>

<p class="title"><br>Beware of GW-BASIC's Random Number Generator

<p>The so-called random-number generators (RNG's) are a topic of great study and 
debate.&nbsp; I say so-called, because their output is only pseudo-random.&nbsp; 
There are those touting &quot;true RNG's,&quot; but others claim that any series of 
numbers that can be programmed or <span class="nobr">repeated &mdash;</span> anything 
created within a <span class="nobr">computer &mdash;</span> cannot be truly random.&nbsp; 
The controversy rages.</p>

<p>Of course, randomness <u>could</u> be achieved by incorporating a seed 
based upon external input, such as the stereo set or static electricity from the 
household cat.&nbsp; Needless to say, that option has been deemed impractical.</p>

<p>Do we care about absolute randomness?&nbsp; Generally not, but it depends 
upon the scope of a project.&nbsp; The GW-BASIC random-number generator starts 
repeating its series after <b>2^24,</b> or <b>16.8</b> million, accesses.&nbsp; 
That limitation presents big problems for someone such attempting a project such 
as the random creation of any of the <b>635 billion</b> possible bridge hands.</p>

<p class="dkgrn">A WORD OF CAUTION</p>

<p>The manuals readily explain that in order to create a repeatable series of 
random numbers, one seeds the RNG with a negative integer.&nbsp; Care is 
essential in doing this, however, because there is a serious anomaly in BASIC's 
RNG relating to the powers of two.&nbsp; Try running this line of code:</p>

<p class="code">10&nbsp; FOR&nbsp; X = 1&nbsp; TO&nbsp; 20:&nbsp; PRINT&nbsp; 
RND(-3 * X),:&nbsp; NEXT</p>

<p>What happens?&nbsp; A lot of the numbers are the same!&nbsp; Now try changing 
the <em>-3</em> to any other negative number.&nbsp; What happens?&nbsp; The 
actual values change, but the pattern remains:</p>

<p class="marg2">The 1<span class="suffix">st</span>, 2<span class="suffix">nd</span>, 
4<span class="suffix">th</span>, 8<span class="suffix">th</span>, and 
16<span class="suffix">th</span> numbers match.<br>
The 3<span class="suffix">rd</span>, 6<span class="suffix">th</span>, and 
12<span class="suffix">th</span> numbers match.<br>
The 5<span class="suffix">th</span>, 10<span class="suffix">th</span>, and 
20<span class="suffix">th</span> numbers match.<br>
The 9<span class="suffix">th</span> and 18<span class="suffix">th</span> numbers match.</p>

<p>Some randomness, eh?&nbsp; Only one of my three BASIC manuals mentions this 
matter, saying that the duplication occurs when the absolute value of the random 
expression becomes a power of two; but that is 
<span class="nobr"><b>incorrect</b> &mdash;</span> or rather, incomplete.&nbsp; 
In fact, duplication occurs for <b>all</b> numbers of the form 
<b class="nobr">-k*2^n</b>, where <b>k</b> is any value and need not be 
an integer.&nbsp; Try it; change the <span class="nobr">-3</span> to something such as 
<span class="nobr">-sin(1.618)</span>.</p>

<p>So what is to be done?&nbsp; Note that when <b>x</b> is odd, there are no matching 
lower values.&nbsp; The only matches are at <b>2x</b>, <b>4x</b>, <b>8x</b>, 
etc.&nbsp; An odd number cannot be a double of any integer.&nbsp; So to be safe, 
simply:</p>

<p class="ind2"><span class="red">Use only odd-numbered negative integers for 
random seeds</span>.</p>

<p>Have a random day!</p>

<!--============(back)============-->
<p class="ctr"><a href="../Computer.htm"><img src="../img/BackArrow1.gif" alt="Go Back"></a></p>

<!--for search--> 
<p class="hide">
Computer programming
<br>GW-BASIC
<br>BASIC programming shortcuts
<br>BASIC utility routines
<br>Random number generator
</p>

</body></html>

