Skip to content

No Two Cities Alike

Bring in RND. Randomise each tower's height and light each window on a coin-flip, and the program produces more than you typed — run it twice, get two different skylines.

  • Commodore 64
  • BASIC

A city where every tower is the same height and every window the same isn’t a city — it’s wallpaper. RND fixes that. Let it choose each tower’s height, and let it flip a coin for each window, and the skyline stops being something you drew and starts being something the program composes.

random-city.bas
10 PRINT CHR$(147)
20 POKE 53281,0
30 FOR C=2 TO 38 STEP 4
40 H=INT(RND(1)*12)+5
50 FOR R=24 TO 24-H+1 STEP -1
60 POKE 1024+R*40+C,160
70 CO=12
80 IF RND(1)<.4 THEN CO=7
90 POKE 55296+R*40+C,CO
100 NEXT R
110 NEXT C
A black C64 screen with a row of towers of varied heights, scattered with lit yellow windows.
RND at work: every tower a different height, windows lit at random. Run it again and you get a different city.

Two changes. Line 40, H = INT(RND(1)*12)+5, gives each tower a random height from 5 to 16: RND(1) is a fraction between 0 and 1, times 12 spreads it across a range, INT chops it to a whole number, and +5 lifts it off the ground so no tower is a stub. Line 80, IF RND(1)<.4 THEN CO=7, lights each window on a coin-flip — about four cells in ten come up lit, scattered instead of striped.

Notice the clamp on the height — *12 ... +5 keeps towers between 5 and 16 tall. Unbounded RND would give you the occasional one-block stub or a tower off the top of the screen. Constrained randomness — random within sensible limits — is what makes the city look designed, not chaotic. That’s the craft of using RND well.

Try this

  • Run it twice. RUN, then RUN again. Each time RND carries on from where it left off, so you get a fresh city every run.
  • Denser windows. Change .4 to .6 and the city blazes; drop it to .2 for a sleepy town.

What’s next

The city composes itself now — but it’s still midnight-black. In Unit 6, two registers set the sky and the border, and the same towers tell three different times of day.

Run it here

This unit's program, built for you.

Program
random-city.prg
Machine
Commodore 64 (PAL Breadbin)

Download random-city.prg