/lcmwc

Lua Persistent CMWC-4096 PRNG

Primary LanguageLua

Build Status license MIT Licence

Lua Persistent CMWC-4096 PRNG

Lua persistent pseudo random number generator. Internally based on CMWC4096 algorithm by George Marsaglia .This algorithm gives good statistical results, it has huge period and it also very performant (original algorithm of course). Includes versions for 32 and 53 bits precision floats.

Cmwc.make( seed: number )

Create new CMWC-4096 PRNG state. Initialization procedure is based on LCG and is borrowed from libtcod sources. Returns state and initial index (1)

Cmwc.rand( state: number[4097], index: number )

Generates unsigned 32-bits integer, returns number, updated state, new index

Cmwc.random32( state: number[4097], index: number, min: number, max: number )

Generates new pseudorandom number and update state. Returns 3 values: number, updated state, next index. This function API tries to somehow mimic one from the original Lua.

When called without arguments, returns a pseudo-random float with uniform distribution in the range [0,1). When called with two integers m and n, math.random returns a pseudo-random integer with uniform distribution in the range [m, n]. (The value n-m cannot be negative and must fit in a Lua integer.) The call math.random(n) is equivalent to math.random(1,n).

Cmwc.random64( state: number[4097], index: number, min: number, max: number )

Takes 2 numbers from the state to create pseudorandom number. This is much more precise than Cmwc.random32 but 2 times slower

Performance

Simple benchmark included with comparsion with builtin randoms is provided. For LuaJIT on my notebook it's x2 performance drop, for vanilla Lua x7. For doubles its x4 and x14.