Balldrop

16K Cassette or Disk
by Bill Boegelein


Interesting programs don't always have to be of eye-straining length; there are some which don't require typing until your fingerprints have vanished. Balldrop is one such program.

Program Listing 1 does manage to accomplish quite a bit in its less than forty BASIC lines. When run, it will simulate a random demo called Balldrop. Imagine a board mounted vertically, on which are arranged in a symmetrical triangular pattern 171 nails. Marbles of equal size and weight are dropped onto the top nail and can randomly bounce either to the left or right each time they come in contact with the nail. If a thousand marbles were dropped, how many would fill each of the nineteen compartments at the bottom?

Inner workings

The program runs surprisingly fast for being written entirely in BASIC. Lines 0-50 set screen color, margins and DIMS, and clear needed variables. The variable MARBLES in Line 50 should be set to the desired number of marbles to be dropped. Lines 100-200 draw the screen. Lines 200-300 drop one marble at a time onto the top peg. Line 240 randomly sets RN to either +1 or -1. When this is added to the X in the POSITION statement, it moves the marble one position left or right, while Line 270 erases its old position. This is a fairly common way to create the pseudo-animation effect seen here. Lines 300-400 keep track of the totals seen at the screen's bottom in the array C(XPOS) and print this total vertically in the corresponding position.

Variations on a theme

Adjust the variable MARBLES in Line 50 to the number of marbles to be dropped in the demonstration. Use CTRL-1 to halt and restart the program where desired. If the program is left running over approximately eight minutes, the Atari will automatically enter the attract mode and rotate the screen through various colors. Hit the space bar to bring things back to normal. The program can be renumbered in any increment, for it contains no internal line references, no GOTOS, GOSUBs or TRAPS. It can also be condensed with multiple statements per line to reduce its overall length and increase its speed slightly.

New and improved

Add the following lines to Listing 1 to include a much faster machine language subroutine to speed up the Balldrop animation.

BALLDRP2.LST is available in ATASCII format.

60 DIM ML$(150)
70 FOR I=1 TO 108:READ BYTE:ML$(I,I)=C
HR$(BYTE):TTL=TTL+BYTE:NEXT I
80 IF TTL<>14002 THEN ? "ERROR IN DATA
 LINES":STOP
90 SPEED=5:ML$(82,82)=CHR$(SPEED):REM
SPEED 1=FAST TO 255=SLOW
220 XXX=USR(ADR(ML$))
230 REM
240 REM
250 REM
260 REM
270 REM
280 REM
290 REM
1000 DATA 104,169,0,141,8,210,169,3,14
1,15,210,169,19,133,203,169
1010 DATA 1,133,204,169,5,141,0,210,16
9,168,141,1,210,165,203,133
1020 DATA 85,165,204,133,84,169,20,32,
164,246,165,203,133,85,165,204
1030 DATA 133,84,169,0,141,1,210,173,1
0,210,74,74,74,74,74,74
1040 DATA 74,201,0,208,2,169,3,24,101,
203,133,203,198,203,198,203
1050 DATA 162,5,202,160,0,136,192,0,20
8,251,224,0,208,244,169,32
1060 DATA 32,164,246,230,204,165,204,2
01,20,208,168,96
Checksum Data
60 DATA 929,60,805,97,959,85,88,91,94,
97,100,103,714,647,995,5864
1030 DATA 489,640,811,725,2665

Then delete Lines 220-290, replacing them with the single statement:

215 XXX=USR(ADR(ML$)):REM THIS LINE RE
PLACE5 LINES 220-290

The variable SPEED in Line 90 can be varied from 1 to 255 to increase the program's running speed. SPEED=1 will produce an effect barely visible, SPEED=255 slows it dramatically.

Archiver's Note: The machine-language routine used here makes a call to the OS EOUTCH (put one character) routine at address $F6A4. This is a no-no on XL and XE systems, so I'm afraid this modification won't work on those systems.

Normal distribution

The program simulates what is known as "normal distribution," and the totals at the screen's bottom can be predicted fairly accurately by drawing what is called a "Pascal's triangle," like this:

            1
           1  1
         1  2  1
        1  3  3  1
      1  4  6  4  1
     1  5 10 10  5  1
   1  6 15 20 15  6  1
  1  7 21 35 35 21  7  1
1  8 28 56 70 56 28  8  1

Each number in the sequence is found by adding the two numbers immediately above it. If this triangle was carried out to the 19th level that is represented in the Balldrop demo, it would read:

1-18-153-816-3060-8568-18564-31824-437
58-48620-43758-31824-18564-8568-3060-8
16-153-18-1

Probability predicts that this should be the result after the demo drops 262,143 marbles. Using a more reasonable sample of 10,001, the result should come very close to:

0-1-6-31-117-327-708-1214-1669-1855-16
69-1214-708-327-117-31-6-1-0

An actual sample run of 10,001 resulted in totals of:

0-3-6-36-126-306-691-1155-1716-1843-16
44-1246-769-292-126-37-5-0-0

A predicted run of 46,663 should be:

0-3--27-145-545-1525-3305-5665-7789-865
5-7789-5665-3305-1525-545-145-27-3-0

The actual run showed:

0-4-27-150-511-1623-3358-5653-7709-850
5-7743-5815-3375-1482-546-126-33-1-0

...All fairly close approximations of the normal distribution curve.

In conclusion

I'll leave it to our avid readers to design a similar 3-D version, in which the marbles are dropped onto a pyramid of suspended pegs, allowing each marble to fall randomly to the bottom. Good luck.

Listing 1

BALLDRP1.LST is available in ATASCII format.

This BASIC listing uses an image to display special characters
Checksum Data
0 DATA 552,889,260,499,560,250,197,420
,689,901,466,858,142,564,945,8192
150 DATA 262,577,303,588,587,442,601,2
5,503,107,304,707,379,95,789,6269
300 DATA 680,629,906,375,386,397,408,6
55,591,167,26,5220

Previous | Contents | Next

Original text copyright 1984 by ANALOG Computing. Reprinted with permission by the Digital ANALOG Archive.