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

Random

[Deleted User][Deleted User] Posts: 0
edited 2006-10-23 23:44 in BASIC Stamp
Hi , I was wondering if anybody knew if you could put a limit on "RANDOM"· , say you only want numers from 0 to 13 . Is it possible to make it so RANDOM could only choose between those number's

thanks,
brian

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-10-22 04:01
    Using the modulo (//) operator will place the number into a range of 0 through n-1, so if you made a random number r, r//14 will return a number between 0 and 13.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • [Deleted User][Deleted User] Posts: 0
    edited 2006-10-22 04:06
    so is r the variable name

    r var byte

    random r//14

    also can you use more than one random in a row, like

    random color
    random character
    random ypos
    random xpos
  • Fe2o3FishFe2o3Fish Posts: 170
    edited 2006-10-22 04:48
    truckwiz said...
    so is r the variable name

    r var byte

    random r//14

    Well, look at the syntax for the RANDOM command in the PBASIC Help file.
    RANDOM [i]variable[/i]
    



    So.... you really do something more like this
    RANDOM r
    r = r // 14
    



    The first line gets a random number and the second line reduces that
    random number to 0 to 13. You don't necessarily have to reassign it
    back to 'r' -- you can use "r // 14" as part of an expression. BUT, if
    you need that value again the MODULO operator will have to be calculated again.
    {EDIT: Oooppsss! After using the modulo operator on the output of RANDOM
    you REALLY don't want to reassign it back to 'r' (in this case) because the value
    of 'r' will be used to seed the random number generator next time you call execute
    RANDOM r. This will then cause your code to use the same 14 numbers from RANDOM
    over and over again. Probably not what you want.}
    truckwiz said...
    also can you use more than one random in a row, like

    random color
    random character
    random ypos
    random xpos

    Yup!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Rusty-
    --
    Rusty Haddock = KD4WLZ = rusty@fe2o3.lonestar.org
    **Out yonder in the Van Alstyne (TX) Metropolitan Area**
    Microsoft is to software what McDonalds is to gourmet cooking

    Post Edited (Fe2o3Fish) : 10/22/2006 5:01:59 AM GMT
  • [Deleted User][Deleted User] Posts: 0
    edited 2006-10-22 12:59
    Heres my screen saver program , what am i doing wrong ??

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    'screen saver program
    color VAR Byte
    xpos VAR Byte
    ypos VAR Byte

    'clear screen
    SEROUT 15,84,1,[noparse][[/noparse]83,0]
    PAUSE 1600
    main:


    · RANDOM color
    · color = color//14
    · RANDOM xpos
    · xpos = xpos//188
    · RANDOM ypos
    · ypos=ypos//255

    'random color,postion sqaure
    SEROUT 15,84,1,[noparse][[/noparse]67,31,COLOR,XPOS,YPOS]
    PAUSE 17
    GOTO main


    thank's Brian

    Post Edited (truckwiz) : 10/22/2006 1:54:17 PM GMT
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-10-22 16:28
    Hi Brian, I would probably generate the random numbers like this

    color VAR Byte
    xpos VAR Byte
    ypos VAR Byte
    rnd VAR Word
    'clear screen
    SEROUT 15,84,1,[noparse][[/noparse]83,0]
    PAUSE 1600
    main:
    · RANDOM rnd
    · color = rnd//14
    · RANDOM rnd
    · xpos = rnd//188
    · RANDOM rnd
    · ypos = rnd//255
    'random color,postion sqaure
    SEROUT 15,84,1,[noparse][[/noparse]67,31,COLOR,XPOS,YPOS]
    PAUSE 17
    GOTO main

    it would still work if you left out the last two RANDOM statements but I think the way it is has the edge in my opinion.

    Jeff T.
  • [Deleted User][Deleted User] Posts: 0
    edited 2006-10-22 16:59
    AWSOME·JEFF!!! , works perfect

    Thanks ,Brian
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-10-22 21:59
    Sorry I wasn't clearer Brian, RANDOM uses a feedback mechanism were the value of a variable before calling RANDOM is used to generate the next value. So this core variable once initialized shouldn't be written to by any other operation besides another RANDOM. So you make a copy of the variable before manipulating it with the modulo or any other operation.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • [Deleted User][Deleted User] Posts: 0
    edited 2006-10-23 23:44
    paul , I noticed that if you look at the if....then statement in the stamp software it uses random in that way , if I had only dug a little deeper , oh well it's works great now !!!

    Thank's
    Brian
Sign In or Register to comment.