Home / Commodore 64 / Phase 0 / Lesson 5
Lesson 5 of 64

Random Encounters

What you'll learn:

  • Seed and use the `RND` function to generate unpredictable events.
  • Combine randomness with loops and decisions to vary program outcomes.
  • Build a simple game mechanic (dice roll / treasure chest) driven by chance.
8% Complete

Lesson 5 – Random Encounters

Every great adventure needs surprises: treasure that sometimes appears, monsters that occasionally pounce, dice that refuse to roll six. On the Commodore 64, randomness arrives via the RND function. Today you’ll teach your programs to embrace chance.

[📷 suggested: screenshot of a BASIC prompt saying “YOU FIND 12 GOLD PIECES!”]


The One-Minute Tour

  • RND(1) returns a floating-point number between 0 and 1.
  • INT and multiplication turn that into dice, treasure, or enemy spawns.
  • Seed once with RND(-TI) so each run feels fresh.

Example Program

NEW
10 RND(-TI)
20 PRINT "ROLLING A D20..."
30 ROLL = INT(RND(1) * 20) + 1
40 PRINT "YOU ROLLED "; ROLL

Sample outputs:

ROLLING A D20...
YOU ROLLED 17
READY.

Run it again and the number changes. Welcome to the realm of chance.

Tip: RND(-TI) copies the current system clock into the random generator. Call it once near the top of your program after the timer has ticked for a moment.


Experiment Section

  • Change the die: INT(RND(1)*6)+1 for a six-sided roll.
  • Generate loot value: GOLD = INT(RND(1)*50)+10.
  • Random chance event: IF RND(1) < 0.15 THEN PRINT "AMBUSHHH!".
  • Combine with variables from Lesson 4: SCORE = SCORE + GOLD when treasure appears.
  • Build a simple countdown: IF RND(1) < 0.05 THEN LIVES = LIVES - 1 inside a loop and watch the tension rise.

[🎥 suggested: clip re-running the program with different rolls and ambush messages]


Concept Expansion

Randomness turns static scripts into games. Later you’ll store tables of possible events in DATA statements and pick entries using random indices. The Assembly track revisits this idea with better generators, but BASIC’s RND is perfect for prototyping surprises.


Game Integration

  • Treasure rooms: IF RND(1)<0.3 THEN PRINT "YOU FIND GOLD".
  • Enemy spawns: randomize both appearance and attack strength in a loop.
  • Procedural maps: roll coordinates to scatter collectibles.
  • Critical hits: IF RND(1) < 0.1 THEN DAMAGE = DAMAGE * 2.

From the Vault

  • Demo Scene 101 — see how randomness and controlled chaos feed into visual and audio effects.

Quick Reference

REM Random basics
RND(-TI)        : REM seed using system clock
X = RND(1)      : REM 0 <= X < 1
R = INT(RND(1)*6)+1 : REM dice 1-6
IF RND(1)<0.2 THEN PRINT "SURPRISE!"
  • RND(0) repeats the last value; RND(1) gives a fresh one.
  • Multiply first, then apply INT to avoid rounding surprises.
  • Seed once per run; repeated seeding inside loops ruins randomness.

What You’ve Learnt

  • How to seed and use BASIC’s random number generator.
  • Turning RND(1) into useful ranges with multiplication and INT.
  • Blending randomness with scoring and decisions for emergent gameplay.
  • Your programs can now surprise players with loot drops, ambushes, and dynamic outcomes.

Next lesson: Simple Animation — move characters smoothly across the screen using loops, cursor control, and timed delays.