This file is part of Timex Sinclair Public Domain Library Tape 1001
. Download the collection to get this file.
This program generates a random bar chart on screen, plotting 50 vertical bars of random height (1–40 pixels each) using the ZX81/TS1000’s PLOT command. After drawing all bars, it calculates and overlays a horizontal mean line across the chart by replaying the column range. The mean value is then printed below the chart using TAB formatting.
Program Analysis
Program Structure
The program divides into four logical phases:
- Initialisation (lines 1–5): Prints the title in inverse video and sets the running total
Tto zero. - Bar drawing (lines 10–70): Iterates
Jfrom 0 to 49 (50 columns). For each column a random heightRis chosen and accumulated intoT; an inner loop plots each pixel of that bar vertically. - Mean line overlay (lines 80–110): After a 100-frame pause, a second pass over the same 50 columns plots a single pixel at row
T/50for each column, drawing a horizontal mean line. - Result display (lines 120–130): Prints the calculated mean value with
TAB 5indentation, then stops.
Key Variables
| Variable | Purpose |
|---|---|
T | Running total of all random heights |
J | Column index (0–49, used as X coordinate) |
R | Random bar height for the current column (1–40) |
K | Row counter for inner bar-drawing loop |
BASIC Idioms and Techniques
- Random integer generation:
INT (RND*40+1)produces a value in the range 1–40 inclusive, a standard Sinclair BASIC idiom. - Pixel-level graphics:
PLOT J,Kuses the ZX81’s low-resolution (64×44) graphics to draw each bar one pixel at a time in the innerFOR Kloop. - Accumulator pattern:
Tis incremented byRon each of the 50 iterations so thatT/50gives the arithmetic mean without needing a separate summation pass. - Deferred annotation: The mean line is drawn in a separate loop (lines 90–110) after a pause, providing a visual reveal effect.
- Inner loop starting at 0:
FOR K=0 TO Ractually plotsR+1pixels (rows 0 through R), making bars one pixel taller than the raw random value; a minor off-by-one to be aware of.
Notable Anomalies
- Off-by-one in bar height: Because
Kruns from 0 toRinclusive, each bar isR+1pixels tall rather thanR. This also means the mean line position (T/50) slightly underestimates the visual midpoint of the bars. - Screen coordinate limits: The ZX81 PLOT area is 64 wide by 44 tall. With
Jreaching 49 andRreaching 40, both axes stay safely within bounds, but bars at maximum height approach the top of the usable area. - Lines 200–210 (SAVE/RUN): These are a loader stub; the inverse-video digit in the filename acts as an auto-run marker. They are not reached during normal execution due to the
STOPat line 130.
Content
Source Code
1 PRINT "%R%A%N%D%O%M% %B%A%R% %C%H%A%R%T"
5 LET T=0
10 FOR J=0 TO 49
20 LET R=INT (RND*40+1)
30 LET T=T+R
40 FOR K=0 TO R
50 PLOT J,K
60 NEXT K
70 NEXT J
80 PAUSE 100
90 FOR J=0 TO 49
100 PLOT J,T/50
110 NEXT J
120 PRINT TAB 5;"MEAN R = ";T/50
130 STOP
200 SAVE "1003%8"
210 RUN
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
