Yes, Im using the low byte. That gives me 0-255 though. I have another variable that contains a value between 0 and 255 and i need the random number to be between 0 and that.
Yes, Im using the low byte. That gives me 0-255 though. I have another variable that contains a value between 0 and 255 and i need the random number to be between 0 and that.
Something like this should work:
Rnd0 var word
Rnd1 var byte
Lim var byte ' your limiting variable (0 - 255)
Random Rnd0
Rnd1 = Rnd0 */ lim
Al.
All progress began with an idea
Ive not seen that operator before. What does it do? Ive had a look at the online manual and it says it "returns the middle 16 bits of the 32-bit result". Im not sure if thats what i want.
I know i could get to the correct value using the following equation
Rnd1 = (Rnd0.byte0 / 255) * lim
That one wont work though because it will be rounded to 0 or 1 when dividing.
Tray to multiply first.I know i could get to the correct value using the following equation
Rnd1 = (Rnd0.byte0 / 255) * lim
That one wont work though because it will be rounded to 0 or 1 when dividing.
Rnd1 = (Rnd0.byte0 * lim)/ 255 [which is the same as Rnd1 = (Rnd0 */ lim)]
Al.
All progress began with an idea
Ahh, I think i understand how */ is working now. Ive tried both ways and they seem to be working for a few seconds then my LED freezes but i think thats to do with something else. Thanks for your help
Ive figured why the LED freezes now. After generating the random number i was using */ and storing the result back in the same variable. It looks like the RANDOM command uses whats already in the variable as a seed and as i was modifying the value to really low numbers, eventually it was hitting 0 and staying there. I added a second variable and its all working now
Bookmarks