Most computers using some form of BASIC language provide the ability to generate random numbers. Readers who have used random numbers in programs recognize that random numbers are usually in intergers (whole numbers), but the program may not require it. This is because the random number generator produces a range of numbers between 0 and something less than 1 (a very small something). The 2068 computer has a number generator that produces 873 numbers from 0 to 872, all between >0 and <1.
The numbers generated by RND are always the same set of 873 numbers. RANDOMIZE n starts the next RND number. If the reader types 10 PRINT RND and runs it, the same number will be displayed each time. The trick is to start the generator with RANDOMIZE n with any number greater than 0 and less than 65537. The reason that n is greater than 0 is that 0 is reserved for a special seeding process.
For a test, key in this program
10 RANDOMIZE n
20 PRINT RND
and run it with different n values. Try it several times with the same value of n and the repeat of each random number will be obvious. Now try n=872 and a number close to 0.99905396 is displayed. This is the largest number I could find. Next try n=873 and the display will be almost 0, my reading was 0.00018310547. These are close to the limits for the generator, it just recycles with larger numbers up to 65536 and the same range of 873 numbers are available.
There is a logical sequense to the numbers generated for RND. The equation is RND=(75*(n+1)-1)/65536 where n ranges from 0 to 872. All numbers produced are less than 1.
When an interger is used by keying INT, the number is a whole number by rounding down. Thus adding 1 will bring it back to the required number. An example is where 6 is required as a result of RND calculation. So INT(RND*6)+1 will give a number of 6 because the largest number, 0.9990536 multiplied by 6 will give 5.9943237 when rounded down and 1 added equals INT 6. The smallest RND number that will give 6 this way is about 0.834259033. Thus there are a range of numbers that will give INT 6. In fact for this example there are about 145 numbers in the generator that will! I have found that for some calculations RND is not always duplicated exactly. In this example RANDOMIZE 728 and then PRINT RND*6 should give 5.0055542. This is because 728 is the smallest number that will seed RND and produce INT 6 in this manner. This was easy to find by trial and error using the equation for RND previously given.
Should the reader wish to interigate all of the numbers available for seeding RND use this program:5
10 FOR n=0 TO 872
20 PRINT (75*(n+1)-1)/65536
30 NEXT n
The reader has probably noticed that RANDOMIZE 0 has not been addressed. RANDOMIZE 0 is a special case so it is not to be used for starting RND in the ordinary way. RANDOMIZE 0 will force the generator into a very acceptable set of random numbers that will not be duplicated except under very rare chance. The computer keeps track of the number of video frames sent out up to 65537 since start up, or from a POKE 23670,0 and POKE 23671,0 to reset, or from starting over after reaching 65537. A video frame takes 0.16 milliseconds, or 0.00016 seconds.
How about a simple program that flips a coin for HEADS or TAILS? This should be a good example of random if enough test are run.
10 LET h=0: LET t=0
15 REM h is for HEADS and t is for TAILS
20 RANDOMIZE 0
30 FOR N=1 TO 200
40 LET r = RND
50 IF r<.5 THEN LET h = h+1
60 IF r>=.5 THEN LET t = t+1
70 PRINT AT 0,5;"HEADS";" ";h
80 PRINT AT 0,20;"TAILS";" ";t
100 PAUSE 20
110 NEXT n
Line 60 is reproduced as printed and will not enter as printed.
This programs self runs for 200 loops, posting the number of times HEADS and TAILS have come up. Lines 50 and 60 set up the range of numbers produced by RND. This does not require an integer number.
A fair consistency of times for HEADS and TAILS will be shown. Because of the small test base of 200 there will be variations.
Now that the explanation of RANDOMIZE, and RND has been given, the readers should be able to produce a program for a guessing game. Guess what the computer has flipped, HEADS or TAILS, and keep a tally of the wins and losses. Send a copy of your program to me at our mailing address and maybe it will get into our newsletter.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.