On an Apple II without a clock card, there is no TIME$ or TIME function in Applesoft BASIC to retrieve the system time. To seed the random number generator (RND), you must use alternative methods that introduce variability based on user input or hardware inputs.
✅ Best Alternative: Use User Input Timing
A common and effective workaround is to measure how long it takes the user to press a key. This introduces variability into the seed value, mimicking randomness.
Example Code:
10 PRINT "PRESS ANY KEY TO CONTINUE...";
20 X = 0
30 POKE 49168, 0 ' Clear keyboard buffer
40 X = X + 1
50 IF PEEK(49152) = 0 THEN GOTO 40 ' Wait for key press
60 SEED = X - 256 * INT(X / 256) ' Mod 256 to keep seed within 0-255
70 RND -SEED ' Seed the random number generator
Explanation:
- Line 30: Clears the keyboard buffer to ensure the loop waits for a fresh key press.
- Lines 40–50: Count how many times the loop runs before the user presses a key. This count (
X) varies based on the user's reaction time.
- Line 60: Computes
X MOD 256 to ensure the seed is within the range of 0–255.
- Line 70: Seeds the random number generator using
RND -SEED.
This method is reliable and works well even on stock Apple II systems without a clock card or paddle joysticks.
✅ Alternative: Use Paddle Inputs (If Available)
If the system has paddle joysticks connected, you can use the PDL(n) function to read their analog values. These values can vary slightly due to electrical noise or user movement, making them suitable for seeding the random number generator.
Example Code:
10 SEED = PDL(0) + PDL(1) ' Combine paddle 0 and 1 values
20 SEED = SEED - 256 * INT(SEED / 256) ' Mod 256
30 RND -SEED
Notes:
PDL(0) and PDL(1) return values from 0 to 255.
- If no paddles are connected, these values may be 0 or static, reducing randomness.
- This method is best used in conjunction with user input timing for better entropy.
🚫 Why Not Use RND(0) Without Seeding?
If you don’t seed the random number generator (RND), Applesoft BASIC will use a default seed. This means the sequence of random numbers will be the same every time the program runs. To achieve true variability, you must reseed the generator using a method like the ones above.
📝 Summary
| Method | Description | Reliability |
|---|
| User Input Timing | Counts loop iterations until a key is pressed | ✅ Highly reliable |
| Paddle Inputs | Uses paddle values (if connected) | ⚠️ Requires hardware |
Default Seed (RND(0)) | Uses built-in default seed | ❌ Not random across runs |
✅ Recommended Final Code (User Input Timing):
10 PRINT "PRESS ANY KEY TO START...";
20 X = 0
30 POKE 49168, 0
40 X = X + 1
50 IF PEEK(49152) = 0 THEN GOTO 40
60 SEED = X - 256 * INT(X / 256)
70 RND -SEED
This approach ensures your program generates a different sequence of random numbers each time it runs, even on an Apple II without a clock card.