View Full Version : masking
11-11-2006, 12:45 PM
dose any one have a chart or a link on how to mask a random number to a number range?
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
Post Edited (Special_K) : 11/11/2006 4:51:44 AM GMT
11-11-2006, 01:52 PM
·· The following thread has code for generating random numbers in a range.· The trick is in using the modulus operator.· Take care.
Parallax Tech Support
11-15-2006, 01:43 PM
i AM USING THIS CODE
drive = randnum // DDIR 'ddir = 7
DEBUG "drive",BIN drive,CR
DEBUG "drive TIME",BIN dTIME,CR
BRANCH drive, [FORward, rightturn1, FORward, backward, forward, leftturn1, halt, FORward ]
but I am getting numbers way out of range
drive can equal 100
11-15-2006, 02:04 PM
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.
11-15-2006, 07:24 PM
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:
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.
Post Edited (TechnoRobbo) : 11/15/2006 11:33:28 AM GMT
11-16-2006, 12:37 AM
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.