You are here

Seed That Apple RND Generator!


Darnell Salyer

Note: This article was scanned using OCR from the Fall 1984 CCCE Newsletter. Please contact us if you identify any OCR errors.
Do your programs use the random number generator of the Apple? Have you "seeded" the generator to be sure the same series of random numbers is not used during each execution on your freshly activated microcomputer?
Writers of many computer assisted instructional programs for chemistry, including commercially available programs, have not taken measures to see that random number selection begins at a randomly chosen place in the fixed sequence of numbers accessed by RND(n). The oversight is quite likely if a program written for some other computer is modified for use on the Apple.
If an instructional program uses RND a variable number of times, as for example in branched tutorials which depend on user's answers or responses, a repetition of the sequence of random numbers used in the program wi.ll not be so readily apparent except when all responses are correct or all are incorrect.
If a CAI program calls for random numbers a fixed number of times regardless of the number of correct or incorrect responses, and if RND is not seeded, then a student user would see the same examples, unknown number, molecular weights or other data values, comments, "readings" in instrument simulations, graphics features, etc. each time he/she sits down at the microcomputer, turns it on, and runs the program. In like manner, two or more users would see identical CAI items upon first executions of a program.
Although dialects of Basic for minicomputers such as the Digital PDP 11/70 and for some micros, e.g. Radio Shack TRS-80, typically provide that a statement such as RANDOM or RANDOMIZE will achieve the seeding for an accompanying RND statement, that same provision was not made for the apple. Unfortunately, this is not made clear in most of the Basic reference manuals this writer has seen. The Atari produces nonrepetitive sequences with RND (Ø) while RND(-TI) for Commodore machines takes its seed from an internal timer.l The assertion that "every time" RND is used with any positive argument a new random number from  Ø to l is generated is incorrect at least with respect to initial start-up. It should also be understood that simply changing the positive values for dummy argument n of RND(n) makes no difference since all give the same sequence upon initial use. 
In an appendix of one widely used manual for the Apple, 2 the intrinsic subroutine executed by CALL -756 is described very briefly. This will place a number between l and 255 in specified memory locations 78 or 79. The call executes as an input and the "seed" is placed when the program user strikes a key. Since most programs will usually contain early input statements, such as those asking for "press return" after directions, the call may easily be added to serve as the input. ,
1120 CALL -756
1130 FOR XX = l to PEEK (78): TA = RND(5): NEXT XX
For the Apple II+ a seed may be obtained from (78) or (79) even without the call, as long as the program has had one or more inputs prior to peeking. In such cases line 1130 alone gives entry to the random number sequence at one of 255 different places. A seed number of values is essentially thrown away and the next value would be the first used by the main program. The seed may be adjusted arithmetically.
In addition to CALL -756 another short machine language program was recently described which seeds the Apple RND and requires a keystroke.3
Other means of seeding:
a) If a program has required entry of a student name, the date, time, numbers or other information, the seed may be taken from an appropriate ASCII or VAL statement.
b) If game paddles are in use a PDL(n) value may be read and used.
c) A separate small auxiliary file containing a seed could be used with the program. The program opens the file, reads the number, increments the number, writes it back into the file and closes. Also, such a file could provide a record of program usage.
The seed obtained by any of these procedures is then used in some manner, as in line 1130 above, before the main program uses RND(n) for the first time. Diskettes with a menu for program selection such as those of the Seraphim project may have the generator seedin9 provision places in the menu to facilitate any program selected.
Sometimes it may not be possible to add the call and/or needed lines, especially if a program is copy protected or unlistable. It may be possible to execute seeding statements in immediate mode or with a short program from the same or another diskette, and then, without turning the computer off, RUNning or BRUNning the main program or the menu.
*Chemistry Department
Eastern Kentucky University
Richmond, KY 40475
1. Mansfield, Richard {ed .. ), "Zones of Unpredictability", in COMPUTE!, 1(11), 146-47 (1983).
2. Poole, Lon et al., Apple II User's Guide, Appendix D, Osborne/McGraw Hill, 1981.
3. Isaacson, Mark, "The Missing Applesoft Random Number", in The Apple Orchard, _!(6), 76-77 (1983).


09/06/84 to 09/10/84