Hi,
I would like the numbers $0h to $Fh to be randomly generated at a speed interval set by an external pot for the speed.
Yes indeed, that is very do-able, probably even with the humblest 08M2 {LE} PICaxe and up to at least 100 times per second.
It must be remembered that the RANDOM command (and any FSR based system) is only "Pseudo Random", but it is only necessary to understand its "Limitations", and Work Around them, when they are significant. IMHO the most important is that because it introduces only one random bit**, it should be executed at least four times (for a nibble output), usually on the same Word, but it could use more Words if a higher degree of "Randomness" were required. Easiest is to use the four low bits (with
AND or
// operators); other bits may give a "different" result/sequence, but I don't believe that will be any more Random (and maybe less).
The next restriction is that it does use only 16 bits, so certain "patterns" cannot occur, and the sequence will eventually repeat (after about 18 hours at one nibble per second). So, "7 7 7 7" and "A A A A"
will occur (exactly once every cycle), but "7 7 7 7 7" etc., and uniquely "0 0 0 0" will NOT. If either of these restrictions is an issue then 32 bits could create up to 8 nibble-repetitions (eventually !) and the cycle time rises to about
13 years at 10 nibbles/sec. Because the cycle is exactly 65535 iterations (not 2^16 = 65536), then using 4x (or even 16x) repeated RANDOMs for each iteration will NOT reduce the cycle period, but e.g. 5x RANDOMs
will. If you do decide that more than 16 bits are required, then the method of combining the Words can be an interesting further discussion.
Probably not an issue for this application, but I do feel that I should warn (again) that a "fixed" Program (with no active inputs) will always generate the same "Random" sequence when it starts. Thus a simple "MP3 player" with a RANDOM playlist might play exactly the same sequence of Songs each morning after it is switched On. But almost any "Human (or external) Intervention" (including the use of a Pot and/or ADC) will very probably solve the issue.
Buzby's solution is quite ingenious and will probably meet all the requirements*. But to be "nit picking", there are two issues I would suggest. Firstly, I prefer a READADC10 which may introduce just enough (internal) noise to create a further random element, whilst READADC may give a continuously stable (8 bit) value (until the pot is adjusted). Secondly, if we are particularly "unlucky", I believe that if
countr takes a value to cause 255 or 257 loops, then the "Random" sequence will enter a repeated loop of only 257 or 255 iterations. Similarly, there are a total of 14 values (including 3 , 5 , 15 , 17 , 51 , 85 , etc.) which will shorten the loop.
*EDIT: I don't know if it's by accident or design, but Busby's code, exactly as written, looks almost Spot On.
The timing loop appears to execute in around 2 ms, so
countr needs to take a value between about 50 and 500. Therefore, multiplying the READADC byte value by 2 should give a range of around 0 to 1022 (ms) and
countr will always be EVEN. The added "minimising" value needs to be about 50 (for 100 ms) and provided it (and the loop exit value) are also Even, will avoid the "nasty" sub-division factors (that are all Odd). Personally, I'd like to introduce a little extra random "noise" and/or a NV stored Seed, and am intrigued that a 64-bit version loop shouldn't repeat until a time longer than the age of the Universe! But there's much more merit in a
Keep
It
Simple
S approach.
** It could be argued that the RANDOM instruction should execute the FSR algorithm 16 times to generate a completely "new" Word, since it would need only a very "simple" additional loop. But PICaxe Basic needed to be a very "compact" language and the single Bit-update is sufficient, e.g. to emulate a Coin Toss (or just 4 loops for our current nibble requirement). Actually, I'm very pleased that it is so simple, because I have found several "unexpected" and useful applications of the RANDOM command (because it is so "fast"), which I might formally document sometime.
Cheers, Alan.