PDA

View Full Version : more random number questions



bartman
- 14th November 2004, 02:14
I want to generate a random number between 0 and 15, but never had the same number come up twice in a row.

I considered it like this:

<b>start:</b>
<b>random rand</b> ' generate the random number
<b>num = rand & $000F</b> ' grab that last 4 bits of the number
<b>if num = last then start</b> ' compare the number to the previously stored number and if it is the same do it again
<b>last = num</b> ' store the number to compare next time


This seems like it should work, but it's not working for me. I am still getting duplicate consecutive random numbers. Is it because I am using a word to store four bits and if that is the case, what is the proper syntax to compare just 0 to 15 on those words?

Thanks,

Bart

bartman
- 14th November 2004, 17:55
Oops. I think the problem here was more physical than coding. With all the chip removal and installing I bumped a LED lose so with that back in place and careful watching it looks like there are no duplicate numbers back to back.

Bart