Question about RANDOM numbers in basic Stamp
kingspud
Posts: 128
Does anyone know how to program the Basic Stamp 2 to generate a random number from 1 to 8?
I read the description and the help file but the numbers I get are huge!
Any help?
Joe
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“Intellectual growth should commence at birth and cease only at death”
Albert Einstein
I read the description and the help file but the numbers I get are huge!
Any help?
Joe
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“Intellectual growth should commence at birth and cease only at death”
Albert Einstein
Comments
use the modulus operator ( // ), modulus will give you the remainder of a division so if you divide a value by 8 the remainder will always be between 0 and 7
eg
result=( huge_number // 8) +1
Jeff T.
Ok... Here is what I got and it works great so enjoy everyone!!!
Thanks again Jeff, that is exactly what I was looking for!
Joe
' {$STAMP BS2}
' {$PBASIC 2.5}
result· VAR···· Word
final··· VAR···· Byte
counter VAR·· byte
·result = 11000······················· ' set initial "seed" value
Main:
FOR counter = 1 TO 10
final =((result//8) +1)
RANDOM result························ ' generate random number
· DEBUG DEC ? final· ···' show the result on screen
PAUSE 50
NEXT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“Intellectual growth should commence at birth and cease only at death”
Albert Einstein
To my understanding, you are trying to basically emulate an 8 sided die? I am not sure if there is anyway to create a truly random seed. The closest I saw was in the sample code. They connected a button, once the power was on, the seed began counting up, pressing the button would cause it to generate random using the latest generated counter as the seed. This method seems halfway almost up to, but not enough I'd feel comfortable using it on the game table.
I know that random numbers is a crazy deep part of mathematics that I have barely scratched the surface of, so I feel silly asking this knowing the answer is probably a no, or a no followed by a lengthy explanation of why I shouldn't expect such things from such simple hardware, but here goes: can you generate a truly random seed on this thing without resorting to timing functions (which seem potentially exploitable).
TL;DR: Mech has his doubts a truly random seed is possible.
Or this. haha
http://cgi.ebay.com/Lotto-Random-Number-Generator-Key-chain-/130317871540?pt=LH_DefaultDomain_0&hash=item1e578ce9b4#ht_500wt_922
Wouldn't that be an analog output? You need something for A/D conversion.