You are absolutely correct! The Apple II (without a clock card) does not have a TIME or TIME$ command that provides the current time. My apologies for that oversight.
The standard way to get a somewhat unpredictable seed for RND in Applesoft BASIC on a stock Apple II is to rely on user interaction timing.
Here's the most common and effective method:
- Start a loop that increments a counter.
- Inside the loop, check if a key has been pressed.
PEEK(-16384) reads the keyboard data register. If its value is greater than 127, a key has been pressed.
- Once a key is pressed, stop the loop. The value of the counter will depend on how long the user took to press the key.
- Clear the keyboard strobe:
POKE -16368,0 to acknowledge the keypress and allow further input.
- Use the counter value (or a value derived from it) to seed
RND. Remember RND(-X) seeds the generator, where X is a positive number.
Here's how you can implement this:
10 REM RANDOMIZE SEED FROM KEYPRESS TIMING
20 PRINT "PRESS ANY KEY TO START..."
30 S = 0
40 IF PEEK(-16384) < 128 THEN S = S + 1: IF S > 32000 THEN S = 1 : GOTO 40
50 KY = PEEK(-16384) : REM OPTIONAL: GET THE KEY CODE
60 POKE -16368,0 : REM CLEAR KEYBOARD STROBE
70 IF S = 0 THEN S = 1 : REM ENSURE SEED IS NOT 0
80 SEEDVAL = S : REM USE THE COUNTER DIRECTLY
90 REM IF YOU WANT TO EMULATE THE "LAST TWO DIGITS" IDEA
100 REM YOU COULD USE MODULO. FOR EXAMPLE, TO GET A VALUE 0-99
110 REM SEEDVAL = (S MOD 100)
120 REM IF SEEDVAL = 0 THEN SEEDVAL = 1 : REM AVOID SEEDING WITH 0
130 PRINT "SEEDING WITH VALUE:"; SEEDVAL
140 DUMMY = RND(-SEEDVAL) : REM SEED THE RANDOM NUMBER GENERATOR
150 REM NOW USE RND(1) TO GET RANDOM NUMBERS
160 PRINT "SOME RANDOM NUMBERS:"
170 FOR I = 1 TO 5
180 PRINT RND(1)
190 NEXT I
Explanation:
- Line 20: Prompts the user.
- Line 30: Initializes a counter
S.
- Line 40: This is the core loop.
PEEK(-16384): Reads the keyboard. If no key is pressed, the value is less than 128.
S = S + 1: Increments the counter.
IF S > 32000 THEN S = 1: Prevents S from overflowing (Applesoft integers go up to 32767) and resets it if the user waits a very long time. This also ensures S doesn't become negative if it wraps around due to extremely long waits (though highly unlikely).
GOTO 40: Loops back until a key is pressed.
- Line 50 (Optional):
KY = PEEK(-16384) reads the ASCII value of the key pressed (plus 128). You could potentially incorporate KY into your SEEDVAL if you wanted more variation, e.g., SEEDVAL = S + KY.
- Line 60:
POKE -16368,0 is crucial. It clears the keyboard strobe, telling the Apple II that the keypress has been processed. Without this, the program might think the same key is still being held down.
- Line 70: Ensures
S is not zero, as RND(-0) behaves like RND(0) (repeats the last random number) rather than seeding.
- Line 80: Assigns the counter
S to SEEDVAL. This value will be different almost every time the program is run, depending on human reaction time.
- Lines 100-120 (Alternative): If you specifically want a seed value in a smaller range, like the 0-99 implied by
RIGHT$(TIME$,2), you can use the MOD operator.
SEEDVAL = (S MOD 100) would give a value from 0 to 99.
- Again, ensure
SEEDVAL isn't 0.
- Line 130: Prints the seed value for verification (optional).
- Line 140:
DUMMY = RND(-SEEDVAL) actually seeds the generator. The return value of RND when seeded is usually discarded (hence DUMMY). SEEDVAL must be positive.
- Lines 160-190: Demonstrate generating random numbers after seeding.
RND(1) generates a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive).
This "timing loop" method is the de facto standard for getting a variable seed on Apple IIs without a clock card and is much more reliable than trying to PEEK arbitrary memory locations, which might not change predictably.