masking
Special_K
Posts: 162
dose any one have a chart or a link on how to mask a random number to a number range?
like this
I need to mask one variable to numbers between 0 and 7 and one from 0 to 14.
should I use the max command?
any ideas?
Post Edited (Special_K) : 11/11/2006 4:51:44 AM GMT
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
·
·· 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
but I am getting numbers way out of range
drive can equal 100
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.
·········· 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
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.