Shop OBEX P1 Docs P2 Docs Learn Events
Random Number Generator — Parallax Forums

Random Number Generator

LuXo12LuXo12 Posts: 31
edited 2008-09-07 01:01 in BASIC Stamp
Hi everyone
I need some help with the random function on basic stamp 2se

My question is if there is a way to generate a random 2 digit number like 1 to 99 is so how would I do it with the random function?

Thanks in advance

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-06 18:03
    First you use the random function to produce a random number between 0 and 65535, then you use the modulus operator (//) to get the remainder of a division in the range you want. If the random variable used by RANDOM is R and you want a result between 1 and 99, you
    would compute "(R // 99) + 1". This would first give you a remainder between 0 and 98, then add one to that to give you a value between
    1 and 99 inclusive.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-09-06 18:04
    RANDOM W0
    B2 = W0 // 99 + 1
    
    
    


    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • LuXo12LuXo12 Posts: 31
    edited 2008-09-07 01:01
    Thanks a lot guys.
Sign In or Register to comment.