RANDOM - BASIC Stamp Command Reference
Demo Program (RANDOM.bs2)
' Connect a button to I/O pin 7 as shown in the figure in the RANDOM command description
' (in the manual) and run this program. This program uses RANDOM to simulate a coin toss.
' After 100 trials, it reports the total number of heads and tails thrown.
'{$STAMP BS2}
Flip
VAR
WORD
Coin
VAR
Flip.BIT0
Trials
VAR
BYTE
Heads
VAR
BYTE
Tails
VAR
BYTE
Btn
VAR
BYTE
Start:
DEBUG CLS, "Press button to start"
FOR Trials = 1 TO 100
Hold:
RANDOM Flip
BUTTON 7, 0, 250, 100, Btn, 0, Hold
branch coin,[head,tail]
Head:
DEBUG CR, "HEADS"
Heads = Heads + 1
GOTO TheNext
Tail:
DEBUG CR, "TAILS"
Tails = Tails + 1
TheNext:
NEXT
' When done, show the total number of heads and tails.
DEBUG CR, CR, "Heads: ", DEC Heads, " Tails: ", DEC Tails
Page 254 • BASIC Stamp Programming Manual 2.0b • www.parallaxinc.com
'STAMP directive (specifies a BS2)
' The random number.
' A single bit of the random number.
' Number of flips.
' Number of throws that came up heads.
' Number of throws that came up tails.
' Workspace for Button instruction.
' 100 tosses of the coin.
' While waiting for button, randomize.
' Wait for button.
' If 0 then head; if 1 then tail.
' Show heads.
' Increment heads counter.
' Next flip.
' Show tails.
' Increment tails counter.
' Next flip.
2
2
2
2
e
p
sx
NOTE: This is written for the BS2
but can be used for the BS2e,
BS2sx and BS2p also. Locate the
proper source code file or modify
the STAMP directive before
downloading to the BS2e, BS2sx or
BS2p.
Need help?
Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?