Wednesday 19 March 2008

Lagged Fibonacci Pseudo-Random Generator

If you need a pseudo-random number generator which works regardless of the coresize, a Lagged Fibonacci generator is one of the better options.

Here's an implementation in 3 instructions with _rndj fixed at 1. _rndk can take the values 3, 4, 6, 7, 15, 22, 60 or 63 depending on the period required. With _rndk = 4 and coresize 8000, the period is 31200.

For more variations, see the Wikipedia article on Lagged Fibonacci generators.
           _rndk  equ 4

random mov.a >rdata, random_out
mod.ab #_rndk, rdata
add.a *stack, @rdata

rdata dat 3711, 0
dat 1674, 0
dat 4323, 0
dat 7835, 0

No comments: