This is only a preview of the April 1998 issue of Silicon Chip. You can view 32 of the 96 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Articles in this series:
Items relevant to "An Automatic Garage Door Opener; Pt.1":
Articles in this series:
Articles in this series:
Items relevant to "40V 8A Adjustable Power Supply; Pt.1":
Articles in this series:
Items relevant to "A Chook Raffle Program For Your PC":
Purchase a printed copy of this issue for $10.00. |
Basic software generates random numbers
A chook raff le
program for your PC
Forget about hats, barrels and old-fashioned
clackety-clack chocolate wheels for your next
chook raffle. This random number generator
runs on a PC and will prevent losing punters
from crying foul.
By RICK WALTERS
Many clubs, schools and other organisations often have raffles and need
to draw winning numbers from a hat
or barrel. Have you ever wondered
whether your ticket butt was actually
in there when you didn’t win a prize?
This random number generator program guarantees that everyone has an
equal chance to win the chook.
By selecting the appropriate range
of numbers, the program can also be
used to select numbers for Lotto.
Design
After looking at the cost of the components necessary to make a display
and the difficulty in generating true
random numbers with discrete logic,
we decided to use a computer to do all
the hard work. A few lines of Basic will
generate true random numbers and a
bit of juggling of a graphic block gives
a readout which is large enough to be
seen quite a distance from the com
puter screen. Additionally, the results
of each draw are saved to the hard disc
and can be recalled and displayed if
necessary.
The program, RAFFLE.BAS (see listing on pages 75-76), requires an EGA
monitor and video card capable of 640
x 480 pixel display. This means that
you may have an old 286 or newer
system lying around which can be
Fig.1:this screen display shows the results of a draw. The program allows you to
select the lowest draw number, the highest draw number, the quantity of prizes
to be drawn, and whether to draw from lowest to highest prize or vice versa
74 Silicon Chip
pressed into service.
The screen display of Fig.1 shows
the results of a draw. The program
allows you to select the lowest draw
number, the highest draw number, the
quantity of prizes to be drawn, and
whether to draw from lowest to highest
prize or vice versa. This draw had the
lowest number as 100, the highest as
5000, 10 prizes and the draw sequence
from low to high.
These parameters are set in lines
1350 and 1360 of the listing, and can
be altered to suit your particular needs.
If you want the draw to start with first
prize then NOSEQ on line 1360 should
be changed to equal 1.
Lines 20-50 control the program
sequence, with line 20 calling the
initialisation routine. This includes
the starting and finishing numbers and
also defines a host of parameters that
will be used. The SC logo and header,
along with the results box ,is drawn by
subroutine 5000.
The real work is done in subroutine
2000, where the random number is
actually generated. If the RND (generate a random number) function was
used, each time the program was run
it would generate the same series of
numbers. To prevent this happening,
line 2030 uses the RANDOMIZE (sorry
about the US spelling) function.
This by itself will prompt you for an
input. However, the last thing we want
in this type of program is for it to ask
the unsuspecting user for input. By
adding TIMER, we force Basic to read
the DOS clock and use this number
as its input. As the timer value increments each second, this will always
have a different value and produce a
different sequence of numbers.
The number generated will always
be between zero and one, so to make
it fit our requirements we have to
introduce the number to start (NOTO
Listing 1: Raffle.bas
1 GOTO 10
5 SAVE “C:\bas\raffle”,A ‘Save file on C drive in ASCII format
6 SAVE “A:\bas\raffle”,A ‘Save file on A drive
7 SAVE “B:\bas\raffle”,A ‘Save file on B drive
10 REM This program draws a raffle & shows the result
11 ‘in large numerals in the top area of the screen
12 ‘When the next prize is drawn the previous draw
13 ‘is recorded in the lower area of the screen
14 ‘Lowest & highest number & number of prizes to draw
15 ‘as well as draw from high to low or low to high
16 ‘can be selected.
17 REM run 5 will save program to drive C
18 REM run 6 will save program to drive A
19 REM run 7 will save program to drive B
20 GOSUB 1000 ‘Initialise
30 GOSUB 5000 ‘Write heading
40 GOSUB 2000 ‘Generate a random number
50 GOSUB 6000 ‘Save draw to hard disk
999 CLS: SYSTEM
1000 ‘***********************
1010 ‘Initialisation routine.
1020 ‘***********************
1030 KEY OFF: SCREEN 9: CLS: DEFINT A-C,R,N: DEFSTR D,E,K,U
1035 ‘A to C,R & N integers, D,E,K,U Are strings, rest single precision
1040 ESC = CHR$(27): ENTER = CHR$(13): KSP = CHR$(32)
‘Spacebar
1140 DEF FNCENTRE$(M$) = SPACE$((79 - LEN(M$))/2) + M$
‘Centre text
1150 DEF FNCEOL$ = STRING$(79 - POS(Q),” “)
1170 ULT = CHR$(218): DLT = CHR$(201): URT = CHR$(191): DRT
= CHR$(187)
1175 ‘Single & Double Left & Right top corners
1180 ULB = CHR$(192): DLB = CHR$(200): URB = CHR$(217): DRB
= CHR$(188)
1185 ‘Single & Double Left & Right bottom corners
1190 UH = CHR$(196): DH = CHR$(205): UV = CHR$(179): DV =
CHR$(186)
1195 ‘Single & Double Horizontal & vertical lines
1350 NOTOSTART = 100: NOTOFIN = 5000
1360 NOTODRAW = 10: NOSEQ = 0 ‘Low to Hi, 1 = Hi to low
1370 DIM DRAWN$(NOTODRAW)
1380 RL = 10: CL = 35: C1 = 10 ‘Row & column for large digits
1390 RP = 17: CP = 3 ‘Row & column for prize listings
1400 RR = RP ‘Row reference for view print in sub 7000
1410 DOB = CHR$(219) ‘8 x 14 block
1420 SLOW = 3000 ‘Delay for poker machine routine. Smaller for
slow machines
1999 RETURN
2000 ‘*************************
2010 ‘Generate a random number.
2020 ‘*************************
2030 RANDOMIZE TIMER
2040 LOCATE 25,1: PRINT FNCENTRE$(“Press SPACEBAR for First
draw”);
2050 K = INPUT$(1): IF K < > KSP THEN 2050
2060 FOR A = 1 TO NOTODRAW
2070 X = INT(RND * (NOTOFIN - NOTOSTART)) + NOTOSTART
‘Generate a number
2080 FOR B = 1 TO A ‘Check to see if this is the same as the first
number drawn
2090 IF VAL(DRAWN$(B)) = X THEN 2070 ‘if so generate a new
number
2100 NEXT B ‘otherwise check the rest
2110 DRAWN$(A) = STR$(X) ‘If not previously drawn add it to the
list
2120 LOCATE 25,1: PRINT FNCEOL$;
2130 GOSUB 3000 ‘Do poker machine style draw
2140 GOSUB 7000 ‘Move number to display area
2150 IF A = NOTODRAW THEN 2190 ‘All numbers drawn
2160 LOCATE 25,1: PRINT FNCENTRE$(“Press SPACEBAR for next
draw”);
2170 K = INPUT$(1): IF K < > KSP THEN 2170
2180 NEXT A ‘Then generate the next number
2190 LOCATE 25,1: PRINT FNCENTRE$(“Press SPACEBAR to save
this draw”);
2200 K = INPUT$(1): IF K < > KSP THEN 2200
2999 RETURN
3000 ‘*************************
3010 ‘Poker machine style draw.
3020 ‘*************************
3030 R = RL: C = CL ‘Restore original row & column values
3040 FOR B = 2 TO LEN(DRAWN$(A))
3050 LOCATE R + 2,10: IF NOSEQ THEN PRINT A; ELSE PRINT
NOTODRAW + 1 - A;
3060 LOCATE R+2,POS(X)-1
3070 IF NOSEQ THEN IF A = 1 THEN PRINT “st”; ELSE IF A = 2 THEN
PRINT “nd”;
3080 IF NOSEQ THEN IF A = 3 THEN PRINT “rd”; ELSE IF A > 3 THEN
PRINT “th”;
3090 IF NOSEQ = 0 THEN IF A = NOTODRAW THEN PRINT “st”; ELSE
IF A = NOTODRAW-1 THEN PRINT “nd”;
3100 IF NOSEQ = 0 THEN IF A = NOTODRAW-2 THEN PRINT “rd”;
ELSE IF A < NOTODRAW-2 THEN PRINT “th”;
3110 PRINT “ Prize “;
3120 GOSUB 4030: GOSUB 3330 ‘Print a 0
3130 GOSUB 4130: GOSUB 3330 ‘Print a 1
3140 GOSUB 4230: GOSUB 3330 ‘Print a 2
3150 GOSUB 4330: GOSUB 3330 ‘Print a 3
3160 GOSUB 4430: GOSUB 3330 ‘Print a 4
3170 GOSUB 4530: GOSUB 3330 ‘Print a 5
3180 GOSUB 4630: GOSUB 3330 ‘Print a 6
3190 GOSUB 4730: GOSUB 3330 ‘Print a 7
3200 GOSUB 4830: GOSUB 3330 ‘Print an 8
3210 GOSUB 4930: GOSUB 3330 ‘Print a 9, then print first digit of
random number
3220 CC = VAL(MID$(DRAWN$(A),B,1))
3230 ON CC + 1 GOSUB 4030,4130,4230,4330,4430,4530,4630,473
0,4830,4930
3240 C = C + 7 ‘Add a space between digits
3250 NEXT ‘Then print the next digit
3260 LOCATE R,C: PRINT FNCEOL$
3270 FOR AA = 1 TO 4: LOCATE CSRLIN,C: PRINT FNCEOL$: NEXT
3299 RETURN ‘Go back to SUB 2000 at line 2140
3300 ‘************************************************
3310 ‘Delay routine to allow numbers to appear slowly.
3320 ‘************************************************
3330 FOR AA = 1 TO SLOW: NEXT: FOR AA = 1 TO SLOW: NEXT
3340 FOR BB = 0 TO 4: LOCATE R + BB,C: PRINT STRING$(7,” “)
3350 NEXT BB
3399 RETURN
4000 ‘*************************************************
*******************
4010 ‘4030 - 4920 draw large block digits from 0 to 9 at the
location R,C.
4020 ‘***********************************************
*********************
continued on page 76
April 1998 75
Listing 1: Raffle.bas
continued from page 75
4030 ‘digit 0
4040 LOCATE R,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT
4050 FOR AA = 1 TO 4: LOCATE CSRLIN,C: PRINT DOB;: LOCATE
CSRLIN,C+4: PRINT DOB: NEXT
4060 LOCATE CSRLIN,C: FOR AA = 1 TO 5: PRINT DOB;: NEXT
4099 RETURN
4120 ‘digit 1
4130 LOCATE R,C+1: PRINT DOB;DOB
4140 FOR AA = 1 TO 3: LOCATE CSRLIN,C+1: PRINT DOB;DOB:
NEXT
4150 LOCATE CSRLIN,C + 1: PRINT DOB;DOB;
4199 RETURN
4220 ‘digit 2
4230 LOCATE R,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT: PRINT
4240 LOCATE CSRLIN,C+3: PRINT DOB
4250 LOCATE CSRLIN,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT:
PRINT
4260 LOCATE CSRLIN,C: PRINT DOB
4270 LOCATE CSRLIN,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT
4299 RETURN
4320 ‘digit 3
4330 LOCATE R,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT: PRINT
4340 LOCATE CSRLIN,C+3: PRINT DOB
4350 LOCATE CSRLIN,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT:
PRINT
4360 LOCATE CSRLIN,C+3: PRINT DOB
4370 LOCATE CSRLIN,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT
4399 RETURN
4420 ‘digit 4
4430 LOCATE R,C: PRINT DOB;DOB: LOCATE CSRLIN,C: PRINT
DOB;DOB
4440 LOCATE CSRLIN,C: PRINT DOB;DOB;SPC(2);DOB
4450 LOCATE CSRLIN,C: FOR AA = 1 TO 6: PRINT DOB;: NEXT:
PRINT
4460 LOCATE CSRLIN,C+4: PRINT DOB;
4499 RETURN
4520 ‘digit 5
4530 LOCATE R,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT: PRINT
4540 LOCATE CSRLIN,C: PRINT DOB
4550 LOCATE CSRLIN,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT:
PRINT
4560 LOCATE CSRLIN,C+3: PRINT DOB
4570 LOCATE CSRLIN,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT
4599 RETURN
4620 ‘digit 6
4630 LOCATE R,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT: PRINT
4640 LOCATE CSRLIN,C: PRINT DOB
4650 LOCATE CSRLIN,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT:
PRINT
4660 LOCATE CSRLIN,C: PRINT DOB; SPC(2);DOB
4670 LOCATE CSRLIN,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT
4699 RETURN
4720 ‘digit 7
4730 LOCATE R,C: FOR AA = 1 TO 5: PRINT DOB;: NEXT: PRINT
4740 FOR AA = 1 TO 3: LOCATE CSRLIN,C+3: PRINT DOB;DOB:
NEXT
4780 LOCATE CSRLIN,C+3: PRINT DOB;DOB;
4799 RETURN
4820 ‘digit 8
4830 LOCATE R,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT
4840 FOR AA = 1 TO 2: LOCATE CSRLIN,C: PRINT DOB;: LOCATE
CSRLIN,C+4: PRINT DOB: NEXT
4850 LOCATE CSRLIN,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT
76 Silicon Chip
4860 FOR AA = 1 TO 2: LOCATE CSRLIN,C: PRINT DOB;: LOCATE
CSRLIN,C+4: PRINT DOB: NEXT
4870 LOCATE CSRLIN,C: FOR AA = 1 TO 5: PRINT DOB;: NEXT
4899 RETURN
4920 ‘digit 9
4930 LOCATE R,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT
4940 FOR AA = 1 TO 2: LOCATE CSRLIN,C: PRINT DOB;: LOCATE
CSRLIN,C+4: PRINT DOB: NEXT
4950 LOCATE CSRLIN,C: FOR AA = 1 TO 4: PRINT DOB;: NEXT
4960 FOR AA = 1 TO 2: LOCATE CSRLIN,C+4: PRINT DOB: NEXT
4970 LOCATE CSRLIN,C: FOR AA = 1 TO 5: PRINT DOB;: NEXT
4999 RETURN
5000 ‘****************
5010 ‘Write to screen.
5020 ‘****************
5030 COLOR 4,11: X = 100: Y = 25: PSET (X,Y) ‘Write SC to
screen
5040 DRAW “u12;h12;l48;g12;d24;f12;r32;d24;l24;u12;l24;d12;
f12;r48”
5050 PSET (X,Y): DRAW “l24;u12;l24;d24;r32;f12;d24;g12”
5060 PAINT (X-20,Y-5) ‘draw & fill S
5070 PSET (X+90,Y)
5080 DRAW “u12;h12;l48;g12;d60;f12;r48;e12;u12;l24;d12;l24;u
60;r24;d12;r24”
5090 PAINT (X+80,Y-5) ‘draw & fill C
5100 COLOR 14,11
5110 LOCATE 3,35: PRINT “Silicon Chip”;
5120 LOCATE 5,35: PRINT “Computerised Chook Raffle
Drawer”;
5130 LOCATE 16,1: PRINT DLT;
5140 FOR J = 2 TO 79: PRINT DH;: NEXT: PRINT DRT;
5150 FOR J = 2 TO 8: PRINT DV;TAB(80);DV;: NEXT
5160 PRINT DLB;: FOR J = 2 TO 79: PRINT DH;: NEXT: PRINT
DRB;
5199 RETURN
6000 ‘******************
6010 ‘Save draw to disk.
6020 ‘******************
6030 D$ = MID$(DATE$,4,2) + LEFT$(DATE$,2) ‘Date
6040 T$ = LEFT$(TIME$,2) + MID$(TIME$,4,2) ‘Time
6050 FILE$ = D$ + T$ + “.DRW” ‘Name file as date + time & add
filetype
6060 OPEN FILE$ FOR OUTPUT AS #1
6070 WRITE# 1, NOSEQ ‘Write Hi to low or low to high
sequence
6080 FOR A = 1 TO NOTODRAW
6090 WRITE# 1, DRAWN$(A) ‘Save the numbers
6100 NEXT A
6110 CLOSE 1
6999 RETURN
7000 ‘************************************
7010 ‘Write draw & number to display area.
7020 ‘************************************
7030 PRIZE$ = “”
7040 FOR CS = 1 TO 4: PRIZE$ = PRIZE$ +
CHR$(SCREEN(R+2,CS+10)): NEXT
7050 VIEW PRINT RR TO 23
7060 LOCATE RP,CP: PRINT PRIZE$;” Prize”;X; ‘Space results by
20 then goto next
7070 CP = CP + 20: IF CP > 65 THEN CP = 3: RP = RP + 1 ‘line
after four entries
7080 VIEW PRINT
7999 RETURN ‘Go back to SUB 2000 at line 2150
START) and the number to finish (NOTOFIN) into the result. This is done
on line 2070. Once we have a value,
we must make sure that it hasn’t been
drawn already and this is done in lines
2080-2100. If this is the case, then we
store it with all the previously drawn
results in a array called DRAWN$ on
line 2110.
We now go to subroutine 3000
where the prize number is printed,
followed by the prize suffix (st, th, etc).
Line 3060 moves the cursor back one
space as Basic puts a space after an
integer before printing a string and “1
st” doesn’t look quite right. The next
four lines (3070- 3100) work out which
direction the sequence runs and print
the appropriate suffix.
Now comes the actual number for
the draw. To add to the suspense,
we print the large digits from 0-9 in
sequence before actually displaying
the correct digit. They should appear
rather slowly and depending upon
the computer you use you may have
to reduce the value in line 1420, or
even delete the second FOR AA = 1
TO SLOW: NEXT on line 3330.
After we print the large digits we
move to subroutine 7000 which records the prize sequence and draw
number in the rectangu
lar box. We
cheat a bit here, to save us going
through the prize number and suffix rigmarole again, by using the
SCREEN(R,C) function to look at what
we actually wrote previously and
building a string called PRIZE$ in line
7040. This is then written in the box
along with the draw number.
Once the NOTODRAW (number to
draw) has been written to the box, a
message appears on line 25 indicating
that this is the case and prompting you
to press the space-bar to save the draw
to disc. This is done in subroutine
6000 where the filename is created
as the date plus the time with a DRW
suffix; ie 17091445.DRW. The draw
sequence as well as all the results are
Fig.2 (top): the software automatically saves the results of each draw and allows
you to view the results of previous draws at any time by typing in the file name.
Fig.3 (above) shows how the results of previous draws are displayed.
saved, to allow unambiguous recovery
of the draw.
Previous draws
Fig.2 shows the screen for selecting
the results of a previous draw. All the
raffle files are listed and when a file
name which appears on the screen is
entered the results will be displayed
as shown in Fig.3.
No print-out routine is included as
the Print Screen function can be used
if a hard copy is needed.
The DRW suffix is not needed when
you enter a filename but if an incorrect
filename is entered, the cursor will
move back to the beginning of the
name, thereby allowing you to see and
correct your error. This draw listing
(PRIZELST.BAS) is not included here
due to space limitations but is available along with the complete Raffle
software on a floppy disc from SILICON
SC
CHIP (see software advert).
April 1998 77
|