Shop OBEX P1 Docs P2 Docs Learn Events
Question about RANDOM numbers in basic Stamp — Parallax Forums

Question about RANDOM numbers in basic Stamp

kingspudkingspud Posts: 128
edited 2010-10-26 22:20 in BASIC Stamp
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

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-10-21 23:10
    Hi,
    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.
  • kingspudkingspud Posts: 128
    edited 2008-10-21 23:28
    Hi Jeff,

    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
  • mechronealmechroneal Posts: 5
    edited 2010-10-25 01:35
    I notice your code spits out the same 'random' string every time. I am guessing it is because the "final =((result//8) +1)" line is just reducing your constant 'result' to 1375, and seeding the random function with this value.

    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.
  • mechronealmechroneal Posts: 5
    edited 2010-10-25 01:37
    Here's somethign I was reading earlier that also ties in: http://www.phanderson.com/stamp/tutorial_1.html
  • LeonLeon Posts: 7,620
    edited 2010-10-25 04:40
    You could use the amplified noise from a zener diode fed into a Stamp input to generate random numbers, by measuring the number of pulses in a given time.
  • CampeckCampeck Posts: 111
    edited 2010-10-26 07:16
    Or one of those radiation detectors. use the time it takes to detect a particle as the seed. lol. I think they are big though. and have up to 400 volts across them. so zener is a little more feasible.

    Or this. haha
    http://cgi.ebay.com/Lotto-Random-Number-Generator-Key-chain-/130317871540?pt=LH_DefaultDomain_0&hash=item1e578ce9b4#ht_500wt_922
  • ercoerco Posts: 20,256
    edited 2010-10-26 09:39
    Timing a user keypress with a VERY fast counting loop (1-8, 1-8, 1-8...) is one of the better ways to get a random number. It's nearly impossible to get consistent or predictable results. You could also measure the light level with a sensitive CdS cell or phototransistor in a likely-to-be shadowed by your hand area near the pushbutton and add that factor in.
  • mechronealmechroneal Posts: 5
    edited 2010-10-26 15:54
    Hrmm.. That sounds feasible. On to Zener Diodes (/wanders off to wikipedia)
  • mechronealmechroneal Posts: 5
    edited 2010-10-26 18:05
    Leon wrote: »
    You could use the amplified noise from a zener diode fed into a Stamp input to generate random numbers, by measuring the number of pulses in a given time.

    Wouldn't that be an analog output? You need something for A/D conversion.
  • LeonLeon Posts: 7,620
    edited 2010-10-26 18:22
    No. The noise is amplified, and clipped if necessary, resulting in a pulse sequence with random intervals between the pulses.
  • mechronealmechroneal Posts: 5
    edited 2010-10-26 22:20
    Haha. I Google how to set this up on the BS2, and wind up back here.
Sign In or Register to comment.