Linear Congruential Generator

De Augusto Baffa Wiki
Ir para navegação Ir para pesquisar
Esta página é uma versão traduzida da página Gerador Congruencial Linear. Sua tradução está 100% completa.
Outros idiomas:
English • ‎português do Brasil

A linear congruential generator (LCG), is an algorithm that produces a sequence of pseudo-random numbers calculated with a piecewise linear equation.

We will use a linear recurrence relation as follows,


Xi=m.Xi1+p

where m and p are two suitably chosen integers.


And the uniform random variates are obtained after scaling,

Ui=Xip


Sample Python Code

1 million attempts using LCG
def pseudo_uniform_good(mult=16807,mod=(2**31)-1,seed=123456789,size=1):
    """
    A reasoanbly good pseudo random generator
    """
    
    U = np.zeros(size)
    x = (seed * mult + 1) % mod
    U[0] = x / mod

    for i in range(1, size):
        x = (x * mult + 1) % mod
        U[i] = x / mod

    return U