Appendix GWorkbench: code and flowcharts
Shannon's experiment in BASIC
Shannon made his later specimens by opening books at random. The same method fits in twenty lines of BASIC, with one sentence standing in for the book.1
For each new letter, Shannon turned to another page, read until he met the letter he had just written, and wrote down the letter that followed.1 Lines 130 to 180 do exactly that. Lines 50 to 70 do the simpler first step: land anywhere and copy the letter, so common letters come up more often.
10 REM SHANNON'S EXPERIMENT, 1948
20 REM OPEN A "BOOK" AT RANDOM AND COPY LETTERS
30 READ T$: L = LEN(T$)
40 PRINT "FIRST ORDER: LETTERS BY HOW OFTEN THEY OCCUR"
50 FOR J = 1 TO 60
60 PRINT MID$(T$, RND(L), 1);
70 NEXT J
80 PRINT: PRINT
90 PRINT "SECOND ORDER: EACH LETTER FOLLOWS THE ONE BEFORE"
100 C$ = MID$(T$, RND(L), 1)
110 FOR J = 1 TO 60
120 PRINT C$;
130 P = RND(L)
140 IF MID$(T$, P, 1) = C$ THEN 170
150 P = P + 1: IF P > L THEN P = 1
160 GOTO 140
170 P = P + 1: IF P > L THEN P = 1
180 C$ = MID$(T$, P, 1)
190 NEXT J
200 PRINT
210 DATA "IN THE SPRING THE RIVER RISES AND THE BOATS COME BACK TO THE LANDING AT THE FOOT OF THE HILL WHERE THE OLD TOWN STARTED AND THE TRAINS STILL RUN ALONG THE WATER"- Line 30 reads the "book," a sentence in the DATA line, and counts its letters.
- Lines 50–70 open it at random 60 times and copy the letter they land on.
- Lines 130–160 open it at random again and read on to the next copy of the last letter.
- Lines 170–180 take the letter after it, which is how likely letter pairs come out.
What it printed
FIRST ORDER: LETTERS BY HOW OFTEN THEY OCCUR
NNAHETGFTD DLG UTLA HMET D RR TSAASGENI ECMEN EHNN DT IAGS
SECOND ORDER: EACH LETTER FOLLOWS THE ONE BEFORE
BOANG TOONSTOND RIN STHE TS WHE TSPRILLON S TOATO WND STONGA real run on this site's BASIC interpreter. Written for the TRS-80 Color Computer's BASIC, where RND(L) picks a whole number from 1 to L; other BASICs pick random numbers differently. With one sentence for a book, the second order already makes common pairs such as TH and HE.
Notes (1)
- 1C. E. Shannon, “A Mathematical Theory of Communication,” Bell System Technical Journal 27, no. 3 (July 1948): 379–423; 27, no. 4 (October 1948): 623–656, https://doi.org/10.1002/j.1538-7305.1948.tb01338.x. Cited at pages 388–389, 389.
Appendix G · page 2 of 3
Turn pages with the ← and → keys.