[−][src]Module randomize::lcg
Functions for Linear Congruential Generators.
An LCG is just one multiply and one add, so it's very fast. However, they're
not much in terms of statistical quality, so you need to be using the u128
version if you want the kind of quality it takes to pass a PRNG test suite.
Accordingly, the LCGs given here aren't actually supported with a struct to
hold the state and all that. It's just the necessary math expression wrapped
in a function.
The Period of an LCG is equal to the numeric range of the state type used.
- lcg8 is for
u8
and has a period of 2^8 - lcg16 is for
u16
and has a period of 2^16 - and so on
Constants
As you might guess, different LCG multipliers give better or worse quality.
There's apparently a thing called a Spectral
Test you can do to rate the
quality of different LCG multipliers, but I don't know much about it. The
pcg
module has a good multiplier for each LCG size.
Also, the additive value that you pick can affect the output stream as well.
This allows some flexibility, because even with a single good multiplier
selected you still have many output streams available to you. The main limit
here is that the value must be odd, but that's quite a bit of freedom.
Again, the pcg
module offers some defaults if you'd like.
There are also some LCG implementations here with pre-baked constants. They're based on the generators used in various games you might have played before. They are "just for fun", and not actually any higher quality than normal for an LCG of their size.
Jump Ahead
It's possible to do some funny math and advance an LCG as if you'd gone through N steps in only log(N) time. There is a jump function available for each size LCG offered. If you'd like to jump "backward" instead of forward that also works, since PRNG sequences are looped. Just cast a signed value into an unsigned value (eg: -5i32 as u32) and jump by that much. It has to go forward the "long way", but you arrive at the right value.
Functions
jump_lcg8 | Gives the |
jump_lcg16 | Gives the |
jump_lcg32 | Gives the |
jump_lcg64 | Gives the |
jump_lcg128 | Gives the |
lcg8 | The |
lcg16 | The |
lcg32 | The |
lcg32_colosseum | The LCG from a game with "colosseum" in its name. |
lcg64 | The |
lcg128 | The |
lcg32_gen4alt | The alternate LCG from the gen4 games. |
lcg32_gen3gen4 | The LCG from the gen3 and gen4 games. |
lcg64_gen5gen6 | The LCG from the gen5 and gen6 games. |