Frequency Histogram

Author(s): Martin Oakes
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v2 n5 (Volume 2, Issue 5)
Date: May 1981
Pages: 4

This program tests how random the ZX80/MicroAce‘s random number generator really is. It counts how often each number is produced by the random number generator. It also demonstrates the technique of scaling graphs.

N is the number of cells, or bars, in the histogram. L is the number of times the program loops and calls the random number generator. Line 20 allows you up to 20 cells. Line 40 produces a random number from 1 to N. Line 50 increments the corresponding cell. Thus if the random number 8 is returned 6 times during the program run, A(8)=6.

Lines 62-66 scale the stored data to make sure it fits on the screen. The program allows 24 characters for each line of the graph. Line 75 makes line numbers 1-9 space correctly. The comma at the end of line 80 ensures that each bar starts in the same place.

This program contains 1 deliberate bug. Try running it with N=20, L=3000. The display file fills up the space required after the program end for the BASIC system to store intermediate variables.

Use this routine as a subroutine to plot data generated by another program with these changes:

Delete lines 10-17, 30, 40, 60.
Change lines 60 & 160 to RETURN.
Determine a value for J from the main program.
Call GO SUB 50 to store.
Fix the number of cells, N.
Call GO SUB 62 to plot.
Don’t forget to define all variables in your main program.

  10 PRINT "CELLS?"
12 INPUT N
14 IF N>20 THEN LET N=20
15 PRINT "LOOPS"
16 INPUT L
17 CLS
20 DIM A(20)
30 FOR I=1 TO L
40 LET J=RND(N)
50 LET A(J)=A(J)+1
60 NEXT I
62 LET S=1
63 FOR I=1 TO N
64 IF A(I)<24 THEN GO TO 66
65 LET S=A(I)/24+1
66 NEXT I
70 FOR I=1 TO N
75 IF I<10 THEN PRINT " ";
80 PRINT I; "="; A(I)
90 LET M=A(I)/S
100 IF M>24 THEN LET M=24
110 FOR K=1 TO M
120 PRINT "▄";
130 NEXT K
140 PRINT
150 NEXT I
155 PRINT "NUM=";N,"LOOPS=";L,"SCALE=";S
160 STOP
SYNTACTIC SUM= 29619

[Editor’s note: as the article says, line 80 should end with a comma: 80 PRINT I; “=”; A(I), – the printed Syntactic Sum includes it.]

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.