Mr RB
Well-Known Member
Does anyone know a good way of generating a random(ish) number very fast and simple, ie minimum number of PIC 16F instructions or minimum steps in a C algorithm that will compile down to a few PIC instructions.
The goals (in order of priority);
* very small code size
* very fast
* minimal ram vars needed (1 byte is good)
* reasonably random
* produces a 2bit or 3bit random number as the result
* the 2 or 3 bits will be in the least sig bits of the working/output register
The randomness does not need to be perfect. A sequence that repeats after a hundred cycles or so would be ok, or more than a hundred would be better.
It is to be used for hobby robots etc that often need to choose between a few options, like turn left/right etc or make random sound 1,2,3,4 etc. You get the idea.
I'd like to avoid the use of multiplications (as a PIC 16F has no hardware multiplier), and avoid multiple cycles/loops etc as used in most of the "standard" simle RNG systems. Something that toggles a bit mask, does a RRF, does an XOR with the previous value etc. As long as it only takes a few simple operations and doesn't need looping or anything that takes up ROM or wastes time. RAM is more available than ROM, so something that saves a handful of ROM words while needing just another RAM byte would be the better choice. My apps usually have a couple of "temp" RAM vars that can be used by anything, so these would be available too.
Or maybe some system that makes use a free running timer, I always have a timer free running at 1:1 prescaler if that helps. Although my preference is not to use a timer.
As always I am open to totally off-the-wall solutions...
The goals (in order of priority);
* very small code size
* very fast
* minimal ram vars needed (1 byte is good)
* reasonably random
* produces a 2bit or 3bit random number as the result
* the 2 or 3 bits will be in the least sig bits of the working/output register
The randomness does not need to be perfect. A sequence that repeats after a hundred cycles or so would be ok, or more than a hundred would be better.
It is to be used for hobby robots etc that often need to choose between a few options, like turn left/right etc or make random sound 1,2,3,4 etc. You get the idea.
I'd like to avoid the use of multiplications (as a PIC 16F has no hardware multiplier), and avoid multiple cycles/loops etc as used in most of the "standard" simle RNG systems. Something that toggles a bit mask, does a RRF, does an XOR with the previous value etc. As long as it only takes a few simple operations and doesn't need looping or anything that takes up ROM or wastes time. RAM is more available than ROM, so something that saves a handful of ROM words while needing just another RAM byte would be the better choice. My apps usually have a couple of "temp" RAM vars that can be used by anything, so these would be available too.
Or maybe some system that makes use a free running timer, I always have a timer free running at 1:1 prescaler if that helps. Although my preference is not to use a timer.
As always I am open to totally off-the-wall solutions...
Last edited: