Faster Random Numbers
Have you ever been frustrated by the RND function? You type in:
LET R=INT (RND*10)+1
to generate random numbers in the range of 1 to 10. While an output of 3, 2, 2, 4, 7, 7, 5, 3 is random, it is not pleasing to have the same number crop up so often. What can we do?
The conventional answer involves taking every random number generated and comparing it to all those generated before it and discarding it if it is a repeat. Although this method works, the computer must do a lot of work. This takes a long time. For example, I used the program in Listing 1 to generate 52 random, non-repeating numbers. In SLOW mode it took 6 minutes and 30 seconds; in FAST it still took 1 minute and 28 seconds. Of course, this time is variable depending on how lucky the computer’s random numbers are at not repeating.
5 REM CONVENTIONAL METHOD
10 DIM Y(52)
20 FOR X=1 TO 52
30 LET Y(X)=INT (RND*52)+1
40 IF X=1 THEN GOTO 80
50 FOR Y=X-1 TO 1 STEP -1
60 IF Y(X)=Y(Y) THEN GOTO 30
70 NEXT Y
80 PRINT Y(X);" ";
90 NEXT X
There has to be a better way.
Listing 2 shows my solution. Since we know all the numbers we want to generate and we simply want the order jumbled, the easiest thing to do is to give the computer a string with all the numbers already in it and have it take random slices of the string. To make the mix a bit more thorough, we can mix up the numbers in the string.
100 REM FASTER RANDOM NUMBERS
110 DIM A$(52,2)
120 LET X$="07252609392027184019103747500644513522383331151143174813124516082842461405243402492936522141300423033201"
130 FOR X=1 TO 52
140 LET R=(INT (RND*(53-X))+1)*2-1
160 IF LEN X$=2 THEN LET A$(X)=X$
170 IF LEN X$=2 THEN GOTO 200
180 LET A$(X)=X$(R TO R+1)
190 LET X$=X$( TO R-1)+X$(R+2 TO )
200 IF VAL A$(X,1)=0 THEN LET A$(X)=A$(X,2)
210 PRINT A$(X);" ";
220 NEXT X
Listing 2 shows how to generate random numbers from 1-52 (useful, e.g., in card games). I started out with the conventional sort in Listing 1 and waited a minute and a half for it to generate the 52 numbers in random order. I then copied that order into the string in line 120. The rest of the program takes random slices of the string according to the random number generated in line 140.
Tests with this method show a dramatic improvement in speed of execution. It took 26 seconds to generate the same 52 random numbers that took more than 6 minutes in SLOW mode by the other method. In FAST, you can have 52 non-repeating random numbers in 4 seconds. Not bad.
Of course, if you would rather not type in a long string, you can have the computer create one with all the numbers you need. The results are not as pleasing, but it is less work. Listing 3 shows how.
10 REM AUTOMATIC RANDOM NUMBERS
20 LET X$=""
30 PRINT "HOW MANY NUMBERS?"
40 INPUT X
50 IF X>99 THEN GOTO 30
60 DIM A$(X,2)
70 CLS
75 FAST
80 FOR I=1 TO
90 LET X$=X$+("0" AND I<10)+STR$ I
100 NEXT I
105 SLOW
110 FOR I=1 TO X
120 LET R=(INT (RND*(X+1-I))+1)*2-1
130 IF LEN X$=2 THEN LET A$(X)=X$
140 IF LEN X$=2 THEN GOTO 170
150 LET A$(I)=X$(R TO R+1)
160 LET X$=X$( TO R-1)+X$(R+2 TO )
170 IF VAL A$(I,1)=0 THEN LET A$(I)=A$(I,2)
180 PRINT A$(I);" ";
190 NEXT I
Polar Lines
“Polar Lines” demonstrates a technique for drawing lines. It can be applied, e.g., to graphic displays, laser effects, land boundary line plotting, trigonometry programs. The symbols used in the program are all standard conventions in mathematics.
Type the program in, RUN, and ENTER. A pair of coordinate axes are drawn. To use the program follow these steps:
- Enter the coordinates of the starting point where the line will begin.
- Enter the coordinates, first the Х coordinate and then the Y coordinate.
- Enter the length of the line (R).
- Enter the angle (THETA) along which the line will be plotted from the starting point. Where each line ends, the next begins. The computer displays each new starting point.
10 REM POLAR LINES BY CHRISTOPHER D. MARSH
20 FAST
30 FOR F=0 TO 31
40 PRINT AT 10,F;"."
50 NEXT F
60 PRINT AT 11,0; "-32"; AT 11,3 0:"31"
70 FOR F=0 TO 21
80 PRINT AT F,16;"."
90 NEXT F
100 PRINT AT 0,17; "21"; AT 21,17;"-22"
110 SLOW
120 PRINT AT 0,0; "START: ";
130 INPUT SX
135 LET SX=INT (SX+.4)
140 PRINT "(";SX;",";
150 INPUT SY
155 LET SY=INT (SY+.4)
160 PRINT SY;") "
170 PRINT AT 1,0;"R=";
180 INPUT R
190 PRINT R
200 PRINT "THETA=";
210 INPUT THETA
220 PRINT THETA
230 LET Q=THETA*PI/180
240 FOR F=0 TO INT (R+.4)
250 LET X=(F*COS Q)+32+SX
260 LET Y=(F*SIN Q)+22+SY
270 PLOT X,Y
280 NEXT F
290 LET SX=S-32
300 LET SY=Y-22
310 PRINT AT 0,7;INT (SX+.4);",";INT (SY+.4);") "
320 PRINT AT 1,0;" ";AT 2,0;" "
330 GOTO 170
Input Anywhere II
Another way of inputting anywhere (Matt Dralle, “Input Anywhere” SYNC 3:6) is shown in the program below. It demonstrates a very nice way of inputting
information into other programs.
This technique can handle alphabetic or numeric data and can accept up to 31 characters. Input is accomplished just like the normal input by entering the alphanumeric data and then pressing ENTER. A delete function can be added without much difficulty.
Setting the values of I and J moves the prompt around. Beware! The larger the value of I, the smaller the input can be. In line 15 any character that you want can be used as prompt.
10 REM REAL PROMPT
20 LET W$=""
30 LET X=10
40 LET Y=13
50 PRINT AT 9,0;"ENTER A 1-19 DIGIT";AT 10,0;"LONG NUMBER. ";
60 PRINT "?"
70 LET Z$=INKEY$
80 IF Z$="" THEN GOTO 70
90 IF Z$=CHR$ 118 THEN GOTO 140
100 PRINT AT X,Y;Z$;"?";
110 LET Y=Y+1
120 LET W$=W$+Z$
130 GOTO 70
140 PRINT AT X+(Y=32),T$(Y<>32);" "
150 PRINT AT 14,5;"YOU ARE ";W$;" YRS OLD."
170 LET W=VAL W$
Screen Strings
“Screen Strings” illustrates the technique of storing the screen display in a string variable. This has the advantage of being able to print almost instantly with one simple PRINT statement. No time consuming FOR-NEXT loops are needed.
To use the program, put the computer in SLOW mode, type RUN 100 and ENTER. When the program stops with the message 0/160, type PRINT P$ to see the stored string.
10 DIM P$(704)
20 LET X=1
30 LET L=PEEK 16396+256*PEEK 16397+1
40 FOR L=L TO L+725
50 IF PEEK L=118 THEN GOTO 80
60 LET P$(X)=CHR$ PEEK L
70 LET X=X+1
80 NEXT L
90 RETURN
100 FOR I=1 TO 22
110 PRINT TAB INT (RND*20+1);"TEST PATTERN"
120 NEXT I
130 FAST
140 GOSUB 10
150 CLS
160 SLOW
Line Notes
10-90: Store a screen image in the string PS. The length of P$ is the number of character positions on the screen: 22 lines by 32 columns equals 704.
50: Excludes the ENTER characters. TS2068 users must substitute 13 for 118.
60: The variable L addresses the display file and stores each character in PS.
100-160: Demo program.
Random Squares
“Random Squares” fills your screen with randomly placed overlapping squares. The results suggest some modern types of art.
Line 30 lets you stop without leaving any incomplete squares by pressing the A key.
With 1K RAM only a limited number of squares can be displayed.
3 REM "SQUARES"
5 DIM A$(4,4)
10 LET A$(1)="▛▀▀▜"
12 LET A$(2)="▌ ▐"
14 LET A$(3)="▌ ▐"
16 LET A$(4)="▙▄▄▟"
20 LET S=INT (RND*29)
22 LET T=INT (RND*17)
25 FOR H=0 TO 3
27 PRINT AT T+H,S;A$(H+1)
28 NEXT H
30 IF INKEY$="A" THEN STOP
35 GOTO 20