btin in RANDOM explanation
bryan mcdonald
Posts: 27
There is a curious variable called 'Btin' in the sample code for the RANDOM command in the 'BASIC Stamp Syntax and Reference Manual Version 2.1'
========= Here is the sample code ============
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
========== end sample code ===========
note in the BUTTON command (in the Hold: loop) the presence of 'Btin' (which I was not familiar with and thought this might be a special nuance of the BUTTON command...)
I think that is supposed to be 'Btn' (at least to code works for me when I change it from 'Btin' to 'Btn' (and a simple seach of the reference manual yields only one instance of 'Btin' and that is in this code snippet)).
If I found a typo, does Parallax have a similar compensation scheme as Donald Knuth (author of the amazing 'Art of Computer Programming') where he awards $2.56 (one hexidecimal dollar) to the first person to find a unique typo?
respectfully submitted, bry
p.s. speaking of random numbers, has anyone figured out a way to seed the random number generator somewhat randomly without user input? (With personal computers, this is accomplished by checking the time of day...but since there is no real time clock built into the stamp, one can't rely on this.) The technique outlined in the reference manual requires a user to push a button...but that requires a user to do something...do you think using the RCTIME could seed (since I'm guessing the output will vary slightly from command to command)? What other algorithms or commands could be used to seed the random number generator?
========= Here is the sample code ============
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
========== end sample code ===========
note in the BUTTON command (in the Hold: loop) the presence of 'Btin' (which I was not familiar with and thought this might be a special nuance of the BUTTON command...)
I think that is supposed to be 'Btn' (at least to code works for me when I change it from 'Btin' to 'Btn' (and a simple seach of the reference manual yields only one instance of 'Btin' and that is in this code snippet)).
If I found a typo, does Parallax have a similar compensation scheme as Donald Knuth (author of the amazing 'Art of Computer Programming') where he awards $2.56 (one hexidecimal dollar) to the first person to find a unique typo?
respectfully submitted, bry
p.s. speaking of random numbers, has anyone figured out a way to seed the random number generator somewhat randomly without user input? (With personal computers, this is accomplished by checking the time of day...but since there is no real time clock built into the stamp, one can't rely on this.) The technique outlined in the reference manual requires a user to push a button...but that requires a user to do something...do you think using the RCTIME could seed (since I'm guessing the output will vary slightly from command to command)? What other algorithms or commands could be used to seed the random number generator?
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
' {$PBASIC 2.5}
'random number seeder by bryan mcdonald. based on code from:
' What's a Microcontroller - PolledRcTimer.bs2
'
'
' schematic
'
'
' R0 = 220 ohm
'P7 <>
/\/\/
|
|
' | |
' | |
' | |
' ___ /
' C = 3300uF ___ \ R1 = 1K ohm
' | /
' | |
' | |
' | |
'
|
' |
' ___
' -
' Vss
'
' it appears the formatting of my schematic gets tweaked when i post it here.
' it's P7 routed to a 220 ohm resistor that is routed to an RC circuit to ground
' (where the R and C are in parallel to gnd). this is the same as figure 5-8 from the
' what is a microcontroller text available from parallax.
'
' using a simple RC circuit and a simple counting loop can create a
' pseudo random seed for the RANDOM number command.
' the algorithm is to basically charge the capicitor for 6 seconds
' by holding the output of pin 7 high for that time. then loop until
' in7=0 (and count how many times it takes to loop through this
' do ... loop routine until in7 = 0).
' for R1 = 1K and C = 3300uF, I got the following 'times' for the RC
' circuit to discharge: 3482, 3481, 3482, 3484, 3484, 3485, 3485
' 3487, 3489, 3488
' I place 'times' in quotes since this is not really a precise time
' and will vary from different BS2's (some are faster than others).
' the key here is to simply get a different number every time the program
' is run (which is a non-trivial task for deterministic algorithms).
' using the counter2 number to seed the RANDOM command could yield different
' sequences. Ideas for future expoloration: use the first digit of counter2
' as the counter time to charge the RC sequence for an even more random
' seqence?
Counter2 VAR WORD
counter VAR NIB
DEBUG CLS
HIGH 7
DEBUG "Capacitor Charging...", CR
FOR counter = 5 TO 0
PAUSE 1000
DEBUG DEC2 counter, CR, CRSRUP
NEXT
DEBUG CR, CR, "Measure decay time now!", CR, CR
INPUT 7
DO
Counter2 = Counter2 + 1
LOOP UNTIL IN7 = 0
DEBUG CR, CR, CR, "The RC decay time was ",
DEC Counter2, CR
END
Post Edited (bryan mcdonald) : 7/4/2008 7:31:07 PM GMT