Ted's Computer World BASIC  Non-Random Numbers
NOTE: This page has been maintained primarily for legacy purposes, because
GW-BASIC will not run on a modern 64-bit operating system.  Certain other
random-number generators might also suffer from the same fault, however.

Beware of GW-BASIC's Random Number Generator

The so-called random-number generators (RNG's) are a topic of great study and debate.  I say so-called, because their output is only pseudo-random.  There are those touting 'true RNGs', but others claim that any series of numbers that can be programmed or repeated — anything created within a computer — cannot be truly random.  The controversy rages.

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

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

A WORD OF CAUTION

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

10 FOR X=1 TO 20: PRINT RND(-3*X),: NEXT

What happens?  A lot of the numbers are the same!  Now try changing the -3 to any other negative number.  What happens?  The actual values change, but the pattern remains:

The 1st, 2nd, 4th, 8th, and 16th numbers match.
The 3rd, 6th, and 12th numbers match.
The 5th, 10th, and 20th numbers match.
The 9th and 18th numbers match.

Some randomness, eh?  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 incorrect or rather, incomplete.  In fact, duplication occurs for all numbers of the form -k*2^n, where k is any value and need not be an integer.  Try it; change the -3 to something such as -sin(1.618).

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

Use only odd-numbered negative primes as random seeds.

One would do well to apply this safety-feature to any programming language.  Have a random day!

Go Back