Random Bar Chart

This file is part of Timex Sinclair Public Domain Library Tape 1001 . Download the collection to get this file.
Date: 198x
Type: Program
Platform(s): TS 1000
Tags: Graphics

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:

  1. Initialisation (lines 1–5): Prints the title in inverse video and sets the running total T to zero.
  2. Bar drawing (lines 10–70): Iterates J from 0 to 49 (50 columns). For each column a random height R is chosen and accumulated into T; an inner loop plots each pixel of that bar vertically.
  3. 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/50 for each column, drawing a horizontal mean line.
  4. Result display (lines 120–130): Prints the calculated mean value with TAB 5 indentation, then stops.

Key Variables

VariablePurpose
TRunning total of all random heights
JColumn index (0–49, used as X coordinate)
RRandom bar height for the current column (1–40)
KRow 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,K uses the ZX81’s low-resolution (64×44) graphics to draw each bar one pixel at a time in the inner FOR K loop.
  • Accumulator pattern: T is incremented by R on each of the 50 iterations so that T/50 gives 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 R actually plots R+1 pixels (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 K runs from 0 to R inclusive, each bar is R+1 pixels tall rather than R. 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 J reaching 49 and R reaching 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 STOP at line 130.

Content

Appears On

Assembled by Tim Ward from many sources. Contains programs 10001 – 10050.

Related Products

Related Articles

Related Content

Image Gallery

Random Bar Chart

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.

People

No people associated with this content.

Scroll to Top