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

masking

Special_KSpecial_K Posts: 162
edited 2006-11-15 16:37 in BASIC Stamp
dose any one have a chart or a link on how to mask a random number to a number range?
like this
  drive = randnum & %1111           'mask off  number between 0-15 



I need to mask one variable to numbers between 0 and 7 and one from 0 to 14.
should I use the max command?
randnum  =  drive MAX 7


any ideas?

Post Edited (Special_K) : 11/11/2006 4:51:44 AM GMT

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-11-11 05:52
    Hello,
    ·
    ·· The following thread has code for generating random numbers in a range.· The trick is in using the modulus operator.· Take care.

    http://forums.parallax.com/showthread.php?p=522895

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Special_KSpecial_K Posts: 162
    edited 2006-11-15 05:43
    i AM USING THIS CODE
    drive = randnum // DDIR     'ddir = 7
      DEBUG "drive",BIN drive,CR
      DEBUG "drive  TIME",BIN dTIME,CR
      BRANCH drive, [noparse][[/noparse]FORward, rightturn1, FORward, backward, forward, leftturn1, halt, FORward ]
      tdone:
      RETURN
    


    but I am getting numbers way out of range
    drive can equal 100
  • Mike GreenMike Green Posts: 23,101
    edited 2006-11-15 06:04
    BIN drive
    This displays the value of drive as a binary number. "100" in binary is the value four which is a correct value for what you're trying to do.
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2006-11-15 11:24
    I addition to Mr. Green's advice let me point out that you have 8 BRANCH choices you'll never get to the eight one with your code:

    ·········· drive·=·randnum·//·DDIR······'ddir·=·7

    Modulus is the remainder of a division therefore the remainder of a division by 7 is 0 to 6 inclusive.

    Try setting DDIR to 8 your results will be 0 to 7 inclusive and thats what the ·"BRANCH" instructions looking for.

    Of course your code uses the "Forward" label 4 times so it won't matter too much in this example, but if you reuse your code elsewhere or decide to trim down you BRANCH statement it will give you problems.
    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 11/15/2006 11:33:28 AM GMT
  • Special_KSpecial_K Posts: 162
    edited 2006-11-15 16:37
    thanks,
    I found the BIN thing last night and changed it forgot to update.
    I will change the ddir to equal 8.
    The branch code has forward 4 times to stack the deck because I want the random motion to be more forward rather then in circles.
Sign In or Register to comment.