RANDOM Examples

BS1 icon BS2 icon BS2e icon BS2sx icon BS2p icon BS2pe icon BS2px icon

Syntax: RANDOM

 

 

 

' RANDOM.BS1
' Connect a button to I/O pin 7 as shown in the figure in the RANDOM
' command description 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 BS1}
' {$PBASIC 1.0}

SYMBOL  Btn             = 7                     ' button input

SYMBOL  flip            = W0                    ' a random number
SYMBOL  coin            = BIT0                  ' a bit from random number
SYMBOL  trials          = B2                    ' number of flips
SYMBOL  heads           = B3                    ' throws that come up heads
SYMBOL  tails           = B4                    ' throws that come up tails
SYMBOL  btnWrk          = B5                    ' workspace for BUTTON


Start:
  DEBUG CLS, "Press the button to toss coin.", CR

Main:
  FOR trials = 1 TO 100                         ' flip coin 100 times

Hold:
    RANDOM flip                                 ' randomize while waiting
    BUTTON Btn, 0, 250, 100, btnWrk, 0, Hold    ' wait for button press
    BRANCH coin, (Head, Tail)                   ' 0 = heads, 1 = tails

Head:
    DEBUG CR, "Heads!"
    heads = heads + 1                           ' increment heads counter
    GOTO Next_Toss

Tail:
    DEBUG CR, "Tails..."
    tails = tails + 1                           ' increment heads counter

Next_Toss:
  NEXT

  DEBUG CR, CR, "Heads: ", #heads, CR, "Tails: ", #tails
  END

NOTE: The example below is written for the BS2 and will run on any of the BS2-family modules. Modify the $STAMP directive (as required) before downloading to the BS2e, BS2sx, BS2p, BS2pe, or BS2px.

' RANDOM.BS2
' Connect a button to I/O pin 7 as shown in the figure in the RANDOM
' command description 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}
' {$PBASIC 2.5}

Btn             PIN     7                       ' button input

flip            VAR     Word                    ' a random number
coin            VAR     flip.BIT0               ' Bit0 of the random number
trials          VAR     Byte                    ' number of flips
heads           VAR     Byte                    ' throws that come up heads
tails           VAR     Byte                    ' throws that come up tails
btnWrk          VAR     Byte                    ' workspace for BUTTON


Start:
  DEBUG CLS, "Press button to start"

Main:
  FOR trials = 1 TO 100                         ' flip coin 100 times

Hold:
    RANDOM flip                                 ' randomize while waiting
    BUTTON Btin, 0, 250, 100, btnWrk, 0, Hold   ' wait for button press
    IF (coin = 0) THEN                          ' 0 = heads, 1 = tails
      DEBUG CR, "Heads!"
      heads = heads + 1                         ' increment heads counter
    ELSE
      DEBUG CR, "Tails..."
      tails = tails + 1                         ' increment tails counter
    ENDIF
  NEXT

Done:
  DEBUG CR, CR, "Heads: ", DEC heads, " Tails: ", DEC tails
  END

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012