Kakemoms wrote:Its a good one, the Lehmer method, but for me a period of 65536 is a little short. [...] My method with three 16-bit oscillators will ensure no repetition within a very long series of numbers.

The three 16-bit oscillators will also repeat at the latest after 65536 calls. Possibly earlier, with a period of a lower power of 2. Then the period of all 3 oscillators can only be the lowest common multiply, not bigger than 65536. So you're not better off at the moment.

A linear congruential generator (multiply-add) with a 32-bit seed and modulus also a power of 2 shouldn't be too hard to build on the 6502, NR suggest:

Code: Select all

`X := (1664525 * X + 1013904223) MOD 2^32`

n+1 n

You only need to produce the lowest 32 bit of the product. 1664525 is %110010110011000001101 in binary, so this requires ten 32 bit-additions and the corresponding shifts of the seed for the multiply and a single 32-bit addition of 1013904223 in the end. *)

As I wrote earlier, a final multiplication should be used to obtain numbers in the range of 0..R-1 by treating the output as 0:32 fixed-point number. The lower bits of this type of generator aren't as random, so modulo is not recommended.

The example above also has a period of 2^32, which may or may not cover your needs.

*) actually, only 6 shifts are necessary here, as shifts modulo 8 can be produces by using the factor shifted by whole bytes... also, the additions needn't be done in order.