Linear Congruential Generator

De Augusto Baffa Wiki
Revisão de 17h00min de 28 de dezembro de 2020 por Abaffa (discussão | contribs) (Criou página com 'CategoryPseudorandom number generators')
Ir para navegação Ir para pesquisar
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,

[math]X_i = m.X_{i-1}+p[/math]

where m and p are two suitably chosen integers.

And the uniform random variates are obtained after scaling,

[math]U_i = \frac{X_i}{p}[/math]


Sample Python Code

commoldura|direita|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

CategoryPseudorandom number generators