--- title: "Random Bar Chart" id: 57014 type: "computer_media" slug: "random-bar-chart" url: "http://localhost/computer_media/random-bar-chart/" markdown_url: "http://localhost/computer_media/random-bar-chart.md" published_at: "2024-10-02T07:38:08+00:00" modified_at: "2026-03-30T21:20:10+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/10/38_RndBr.png" excerpt: "Fifty random bars plotted pixel by pixel, then a calculated mean line drawn across them — a compact statistics visualiser in under 130 lines of BASIC." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" genre: - name: "Graphics" slug: "graphics" taxonomy: "genre" url: "http://localhost/type/graphics/" media_contents: - id: 56732 title: "Timex Sinclair Public Domain Library Tape 1001" type: "computer_media" url: "http://localhost/computer_media/timex-sinclair-public-domain-library-tape-1001/" media_type: "Program" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/10/38_RndBr.png" media_type_tags: "Graphics" --- # Random Bar Chart 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 | 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,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. ## 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 ```