Shop OBEX P1 Docs P2 Docs Learn Events
Help - Simple RANDOM program not giving me what I want. — Parallax Forums

Help - Simple RANDOM program not giving me what I want.

SN96SN96 Posts: 318
edited 2005-11-13 22:28 in BASIC Stamp
Hello
·
I have finished the "What's a microcontroller" Book and I want to start expanding on my PBASIC knowledge. I want to in the future,·be able to program my bot to have random behaviors. For example, I want to be able to have, as an example,·10 different behavior modes, and by using the RANDOM command, have those behaviors executed based on SELECTCASE values.
·
The problem I am having is: instead of retuning a random number from 0-9, I get values as high as 13 in the debug window? Am I not using the proper modifier? Please see my code bellow.

' {$STAMP BS2}
' {$PBASIC 2.5}
 
Result     VAR      Nib
result = 9             'Set seed value of 9
 
DO
  RANDOM Result
  DEBUG DEC ? Result   'Print value to debug window
  PAUSE 200
LOOP


The code above is an experiment I used to try and understand how RANDOM works.
·
Thanks in advance, I appreciate any help.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike


"Don't always think outside the box, sometimes thinking inside the box is more pratical and simple."

Comments

  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-11-13 22:25
    Make the variable aw ord or at least byte size. A nib is too small to get good random results.

    DEBUG ? result//10
    BRANCH result//10, [noparse][[/noparse]behavior0, behavior1,... ,behavior9]


    That will display and branch on random numbers in the range of 0 to 9.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • SN96SN96 Posts: 318
    edited 2005-11-13 22:28
    Walla! ... it worked! Thanks!



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike


    "Don't always think outside the box, sometimes thinking inside the box is more pratical and simple."
    ·
Sign In or Register to comment.