--- title: "Security Code" id: 56993 type: "computer_media" slug: "security-code" url: "http://localhost/computer_media/security-code/" markdown_url: "http://localhost/computer_media/security-code.md" published_at: "2024-10-02T06:52:22+00:00" modified_at: "2026-03-30T21:20:24+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/10/18_SecCd.png" excerpt: "A week's worth of random four-digit security codes, one per day, generated and sent straight to the printer with a neat rejection-sampling trick." 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: "Entertainment" slug: "entertainment" taxonomy: "genre" url: "http://localhost/type/entertainment/" 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/18_SecCd.png" - url: "http://localhost/wp-content/uploads/2024/10/18_SecC1.png" media_type_tags: "Entertainment" --- # Security Code This program generates and prints daily security codes for a full week, outputting results to both screen and printer via LPRINT. It prompts the user to enter a day/week reference, then calls a subroutine seven times (lines 100–220) to produce a four-digit random code for each day of the week. The code generation subroutine at line 300 uses INT(10000*RND) with a rejection loop at line 310 to ensure the result is always a four-digit number (1000–9999). *** ## Program Analysis ### Program Structure The program is divided into three logical sections: 1. **Initialisation and input (lines 5–18):** Prints a heading in inverse video to both screen and printer, prompts for a day/week label, and makes the first GOSUB call to the code generator. 2. **Code output loop (lines 100–230):** Seven sequential GOSUB 300 calls, each followed by an LPRINT statement labelling the day of the week and the generated code stored in `C`. Execution ends at `STOP` on line 230. 3. **Code generator subroutine (lines 300–320):** Generates a random integer and validates it before returning. Lines 375, 400, and 500 are utility lines sitting beyond the `STOP` at line 230 and beyond the subroutine — they are never reached during normal execution. ### Code Generation Subroutine The subroutine at line 300 uses a rejection-sampling pattern to guarantee a four-digit result: - Line 300: `LET C=INT(10000*RND)` — produces an integer in the range 0–9999. - Line 310: `IF C<1000 THEN GOTO 300` — rejects any value below 1000, looping until a value in the range 1000–9999 is obtained. - Line 320: `RETURN` — passes the validated code back in variable `C`. This is a clean and correct approach; the probability of rejection on any single attempt is 10%, so the average number of iterations per code is approximately 1.11, making it efficient in practice. ### Input and Output Strategy The program mirrors output to both the display (via `PRINT`) and the printer (via `LPRINT`). The heading on lines 6–7 uses inverse-video characters to produce a bold title effect on screen. The user-supplied day/week label `D` is echoed back on line 11 (screen) and incorporated into the LPRINT output on line 12, giving the printed sheet a header that identifies the week. Note that lines 9 and 12 use a trailing semicolon after the prompt string — line 9 to suppress a newline before the INPUT cursor, and line 12 to place `D` on the same LPRINT line as the label text. ### Variable Usage | Variable | Purpose | | --- | --- | | `D` | User-entered day/week identifier (numeric INPUT) | | `C` | Current generated security code (set in subroutine, read by LPRINT) | ### Notable Techniques and Idioms - The inverse-video title string (e.g. `"%D%A%I%L%Y%..."`) produces a highlighted heading without any POKE or attribute manipulation. - The rejection loop in the subroutine is a standard Monte Carlo technique adapted for constrained random number generation in BASIC. - The seven GOSUB calls are written sequentially rather than using a FOR–NEXT loop with a DATA/READ structure for day names; this is simpler but more verbose and harder to maintain. ### Utility Lines and Save Block Line 375 contains a standalone `CLEAR` and line 500 a standalone `RUN`; neither is reachable during normal execution. Line 400 uses `SAVE "1001%8"`, where `%8` is an inverse-video digit 8 in the filename — this acts as an auto-run flag so the program starts automatically when loaded. ### Bugs and Anomalies - Line 10 uses `INPUT D` where `D` is a numeric variable. Entering a non-numeric value (e.g. “12 JAN”) will cause an error. A string variable (`D$`) would be more appropriate for a date label. - The first GOSUB 300 is called at line 18, before any day label is printed; the generated code is immediately consumed by the `LPRINT "SUNDAY..."` at line 100, so this is functionally correct but the placement is slightly misleading — the call logically belongs between lines 90 and 100. ## Source Code ``` 5 REM DAILY SECURITY CODES 6 PRINT "%D%A%I%L%Y% %S%E%C%U%R%I%T%Y% %C%O%D%E%S" 7 LPRINT "%D%A%I%L%Y% %S%E%C%U%R%I%T%Y% %C%O%D%E%S" 8 PRINT 9 PRINT "ENTER DAY/WEEK OF: "; 10 INPUT D 11 PRINT D 12 LPRINT "ENTER DAY/WEEK OF: ";D 18 GOSUB 300 100 LPRINT "SUNDAY: ";C 110 GOSUB 300 120 LPRINT "MONDAY: ";C 130 GOSUB 300 140 LPRINT "TUESDAY: ";C 150 GOSUB 300 160 LPRINT "WEDNESDAY: ";C 170 GOSUB 300 180 LPRINT "THURSDAY: ";C 190 GOSUB 300 200 LPRINT "FRIDAY: ";C 210 GOSUB 300 220 LPRINT "SATURDAY: ";C 230 STOP 300 LET C=INT (10000*RND) 310 IF C<1000 THEN GOTO 300 320 RETURN 375 CLEAR 400 SAVE "1001%8" 500 RUN ```