<s>
A	O
linear	B-Algorithm
congruential	I-Algorithm
generator	I-Algorithm
(	O
LCG	B-Algorithm
)	O
is	O
an	O
algorithm	O
that	O
yields	O
a	O
sequence	O
of	O
pseudo-randomized	O
numbers	O
calculated	O
with	O
a	O
discontinuous	O
piecewise	O
linear	B-Architecture
equation	O
.	O
</s>
<s>
The	O
method	O
represents	O
one	O
of	O
the	O
oldest	O
and	O
best-known	O
pseudorandom	B-Algorithm
number	I-Algorithm
generator	I-Algorithm
algorithms	O
.	O
</s>
<s>
If	O
c	O
=	O
0	O
,	O
the	O
generator	O
is	O
often	O
called	O
a	O
multiplicative	B-Algorithm
congruential	I-Algorithm
generator	I-Algorithm
(	O
MCG	O
)	O
,	O
or	O
Lehmer	B-Algorithm
RNG	I-Algorithm
.	O
</s>
<s>
If	O
c≠0	O
,	O
the	O
method	O
is	O
called	O
a	O
mixed	B-Algorithm
congruential	I-Algorithm
generator	I-Algorithm
.	O
</s>
<s>
When	O
c≠0	O
,	O
a	O
mathematician	O
would	O
call	O
the	O
recurrence	O
an	O
affine	B-Algorithm
transformation	I-Algorithm
,	O
not	O
a	O
linear	B-Architecture
one	O
,	O
but	O
the	O
misnomer	O
is	O
well-established	O
in	O
computer	O
science	O
.	O
</s>
<s>
The	O
Lehmer	B-Algorithm
generator	I-Algorithm
was	O
published	O
in	O
1951	O
and	O
the	O
Linear	B-Algorithm
congruential	I-Algorithm
generator	I-Algorithm
was	O
published	O
in	O
1958	O
by	O
W	O
.	O
E	O
.	O
Thomson	O
and	O
A	O
.	O
Rotenberg	O
.	O
</s>
<s>
A	O
benefit	O
of	O
LCGs	B-Algorithm
is	O
that	O
an	O
appropriate	O
choice	O
of	O
parameters	O
results	O
in	O
a	O
period	O
which	O
is	O
both	O
known	O
and	O
long	O
.	O
</s>
<s>
Although	O
not	O
the	O
only	O
criterion	O
,	O
too	O
short	O
a	O
period	O
is	O
a	O
fatal	O
flaw	O
in	O
a	O
pseudorandom	B-Algorithm
number	I-Algorithm
generator	I-Algorithm
.	O
</s>
<s>
While	O
LCGs	B-Algorithm
are	O
capable	O
of	O
producing	O
pseudorandom	B-Error_Name
numbers	I-Error_Name
which	O
can	O
pass	O
formal	O
tests	O
for	O
randomness	O
,	O
the	O
quality	O
of	O
the	O
output	O
is	O
extremely	O
sensitive	O
to	O
the	O
choice	O
of	O
the	O
parameters	O
m	O
and	O
a	O
.	O
</s>
<s>
Historically	O
,	O
poor	O
choices	O
for	O
a	O
have	O
led	O
to	O
ineffective	O
implementations	O
of	O
LCGs	B-Algorithm
.	O
</s>
<s>
A	O
particularly	O
illustrative	O
example	O
of	O
this	O
is	O
RANDU	B-Algorithm
,	O
which	O
was	O
widely	O
used	O
in	O
the	O
early	O
1970s	O
and	O
led	O
to	O
many	O
results	O
which	O
are	O
currently	O
being	O
questioned	O
because	O
of	O
the	O
use	O
of	O
this	O
poor	O
LCG	B-Algorithm
.	O
</s>
<s>
This	O
is	O
the	O
original	O
Lehmer	B-Algorithm
RNG	I-Algorithm
construction	O
.	O
</s>
<s>
Choosing	O
m	O
to	O
be	O
a	O
power	O
of	O
2	O
,	O
most	O
often	O
m	O
=	O
232	O
or	O
m	O
=	O
264	O
,	O
produces	O
a	O
particularly	O
efficient	O
LCG	B-Algorithm
,	O
because	O
this	O
allows	O
the	O
modulus	O
operation	O
to	O
be	O
computed	O
by	O
simply	O
truncating	O
the	O
binary	O
representation	O
.	O
</s>
<s>
The	O
spectral	B-Algorithm
test	I-Algorithm
is	O
one	O
of	O
the	O
most	O
important	O
tests	O
.	O
</s>
<s>
If	O
a	O
pseudorandom	B-Error_Name
number	I-Error_Name
less	O
than	O
r	O
is	O
desired	O
,	O
is	O
a	O
much	O
higher-quality	O
result	O
than	O
X	O
mod	O
r	O
.	O
Unfortunately	O
,	O
most	O
programming	O
languages	O
make	O
the	O
latter	O
much	O
easier	O
to	O
write	O
(	O
X	O
%	O
r	O
)	O
,	O
so	O
it	O
is	O
the	O
more	O
commonly	O
used	O
form	O
.	O
</s>
<s>
Specifically	O
,	O
if	O
Y	O
is	O
the	O
prototypical	O
series	O
defined	O
by	O
Y0	O
=	O
0	O
and	O
Yn+1	O
=	O
aYn+1	O
modm	O
,	O
then	O
a	O
general	O
series	O
Xn+1	O
=	O
aXn+c	O
modm	O
can	O
be	O
written	O
as	O
an	O
affine	B-Algorithm
function	I-Algorithm
of	O
Y	O
:	O
</s>
<s>
The	O
following	O
table	O
lists	O
the	O
parameters	O
of	O
LCGs	B-Algorithm
in	O
common	O
use	O
,	O
including	O
built-in	O
rand( )	O
functions	O
in	O
runtime	B-Library
libraries	I-Library
of	O
various	O
compilers	B-Language
.	O
</s>
<s>
7.1.6	O
parameters	O
from	O
Knuth	O
and	O
H	O
.	O
W	O
.	O
Lewis	O
232	O
1664525	O
1013904223	O
Borland	O
C/C	O
++	O
232	O
22695477	O
1	O
bits	O
30	O
..	O
16	O
in	O
rand( )	O
,	O
30	O
..	O
0	O
in	O
lrand( )	O
glibc	B-Language
(	O
used	O
by	O
GCC	B-Application
)	O
Implementation	O
in	O
glibc-2.26	O
release	O
.	O
</s>
<s>
See	O
the	O
code	O
after	O
the	O
test	O
for	O
"	O
TYPE_0	O
"	O
;	O
the	O
GNU	B-Language
C	I-Language
library	I-Language
's	O
rand( )	O
in	O
stdlib.h	B-Language
uses	O
a	O
simple	O
(	O
single	O
state	O
)	O
linear	B-Algorithm
congruential	I-Algorithm
generator	I-Algorithm
only	O
in	O
case	O
that	O
the	O
state	O
is	O
declared	O
as	O
8	O
bytes	O
.	O
</s>
<s>
231	O
1103515245	O
12345	O
bits	O
30	O
..	O
0	O
ANSI	O
C	O
:	O
Watcom	B-Application
,	O
Digital	B-Language
Mars	I-Language
,	O
CodeWarrior	B-Operating_System
,	O
IBM	B-Language
VisualAge	I-Language
C/C	O
++C90	O
,	O
C99	B-Language
,	O
C11	O
:	O
Suggestion	O
in	O
the	O
ISO/IEC	O
9899	O
,	O
C17	O
231	O
1103515245	O
12345	O
bits	O
30	O
..	O
16	O
Borland	B-Language
Delphi	I-Language
,	O
Virtual	B-Application
Pascal	I-Application
232	O
134775813	O
1	O
bits	O
63	O
..	O
32	O
of	O
(	O
seed	O
×	O
L	O
)	O
Turbo	B-Application
Pascal	I-Application
232	O
134775813	O
(	O
808840516	O
)	O
1	O
Microsoft	B-Application
Visual/Quick	I-Application
C/C	I-Application
++	I-Application
232	O
214013	O
(	O
343FD16	O
)	O
2531011	O
(	O
269EC316	O
)	O
bits	O
30	O
..	O
16	O
Microsoft	B-Language
Visual	I-Language
Basic	I-Language
(	O
6	O
and	O
earlier	O
)	O
1140671485	O
(	O
43FD43FD16	O
)	O
12820163	O
(	O
C39EC316	O
)	O
RtlUniform	O
from	O
Native	O
APIIn	O
spite	O
of	O
documentation	O
on	O
MSDN	O
,	O
RtlUniform	O
uses	O
LCG	B-Algorithm
,	O
and	O
not	O
Lehmer	O
's	O
algorithm	O
,	O
implementations	O
before	O
Windows	B-Application
Vista	I-Application
are	O
flawed	O
,	O
because	O
the	O
result	O
of	O
multiplication	O
is	O
cut	O
to	O
32	O
bits	O
,	O
before	O
modulo	O
is	O
applied	O
231	O
−	O
1	O
2147483629	O
(	O
7FFFFFED16	O
)	O
2147483587	O
(	O
7FFFFFC316	O
)	O
Apple	B-Operating_System
CarbonLib	I-Operating_System
,	O
11	O
's	O
minstd_rand0	O
,	O
MATLAB	O
's	O
v4	O
legacy	O
generator	O
mcg16807	O
231	O
−	O
1	O
16807	O
0	O
see	O
MINSTD	O
11	O
's	O
minstd_rand	O
231	O
−	O
1	O
48271	O
0	O
see	O
MINSTD	O
MMIX	O
by	O
Donald	O
Knuth	O
264	O
6364136223846793005	O
1442695040888963407	O
Newlib	B-Language
264	O
6364136223846793005	O
1	O
bits	O
62	O
..	O
32	O
(	O
46	O
..	O
32	O
for	O
16-bit	O
int	O
)	O
Musl	B-Language
264	O
6364136223846793005	O
1	O
bits	O
63	O
..	O
33	O
VMS	B-Operating_System
's	O
MTH$RANDOM	O
,	O
old	O
versions	O
of	O
glibc	B-Language
232	O
69069	O
(	O
10DCD16	O
)	O
1	O
Java	B-Language
's	O
java.utilRandom	O
,	O
POSIX	O
[ln]rand48,	O
glibc	B-Language
 [ ln ] rand48 [ _r ] 	O
248	O
25214903917	O
(	O
5DEECE66D16	O
)	O
11	O
bits	O
47	O
..	O
16random0Stephen	O
J	O
.	O
Chapman	O
.	O
</s>
<s>
As	O
shown	O
above	O
,	O
LCGs	B-Algorithm
do	O
not	O
always	O
use	O
all	O
of	O
the	O
bits	O
in	O
the	O
values	O
they	O
produce	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
Java	B-Language
implementation	O
operates	O
with	O
48-bit	O
values	O
at	O
each	O
iteration	O
but	O
returns	O
only	O
their	O
32	O
most	O
significant	O
bits	O
.	O
</s>
<s>
LCGs	B-Algorithm
that	O
use	O
this	O
truncation	O
technique	O
produce	O
statistically	O
better	O
values	O
than	O
those	O
that	O
do	O
not	O
.	O
</s>
<s>
LCGs	B-Algorithm
are	O
fast	O
and	O
require	O
minimal	O
memory	O
(	O
one	O
modulo-m	O
number	O
,	O
often	O
32	O
or	O
64	O
bits	O
)	O
to	O
retain	O
state	O
.	O
</s>
<s>
LCGs	B-Algorithm
are	O
not	O
intended	O
,	O
and	O
must	O
not	O
be	O
used	O
,	O
for	O
cryptographic	O
applications	O
;	O
use	O
a	O
cryptographically	B-Algorithm
secure	I-Algorithm
pseudorandom	I-Algorithm
number	I-Algorithm
generator	I-Algorithm
for	O
such	O
applications	O
.	O
</s>
<s>
Although	O
LCGs	B-Algorithm
have	O
a	O
few	O
specific	O
weaknesses	O
,	O
many	O
of	O
their	O
flaws	O
come	O
from	O
having	O
too	O
small	O
a	O
state	O
.	O
</s>
<s>
A	O
LCG	B-Algorithm
with	O
large	O
enough	O
state	O
can	O
pass	O
even	O
stringent	O
statistical	O
tests	O
;	O
a	O
modulo-2	O
LCG	B-Algorithm
which	O
returns	O
the	O
high	O
32	O
bits	O
passes	O
TestU01	B-Algorithm
's	O
SmallCrush	O
suite	O
,	O
and	O
a	O
96-bit	O
LCG	B-Algorithm
passes	O
the	O
most	O
stringent	O
BigCrush	O
suite	O
.	O
</s>
<s>
One	O
flaw	O
specific	O
to	O
LCGs	B-Algorithm
is	O
that	O
,	O
if	O
used	O
to	O
choose	O
points	O
in	O
an	O
n-dimensional	O
space	O
,	O
the	O
points	O
will	O
lie	O
on	O
,	O
at	O
most	O
,	O
hyperplanes	O
(	O
Marsaglia	O
's	O
theorem	O
,	O
developed	O
by	O
George	O
Marsaglia	O
)	O
.	O
</s>
<s>
The	O
spectral	B-Algorithm
test	I-Algorithm
,	O
which	O
is	O
a	O
simple	O
test	O
of	O
an	O
LCG	B-Algorithm
's	O
quality	O
,	O
measures	O
this	O
spacing	O
and	O
allows	O
a	O
good	O
multiplier	O
to	O
be	O
chosen	O
.	O
</s>
<s>
Another	O
flaw	O
specific	O
to	O
LCGs	B-Algorithm
is	O
the	O
short	O
period	O
of	O
the	O
low-order	O
bits	O
when	O
m	O
is	O
chosen	O
to	O
be	O
a	O
power	O
of	O
2	O
.	O
</s>
<s>
Nevertheless	O
,	O
for	O
some	O
applications	O
LCGs	B-Algorithm
may	O
be	O
a	O
good	O
option	O
.	O
</s>
<s>
Similarly	O
,	O
in	O
an	O
environment	O
such	O
as	O
a	O
video	B-Device
game	I-Device
console	I-Device
taking	O
a	O
small	O
number	O
of	O
high-order	O
bits	O
of	O
an	O
LCG	B-Algorithm
may	O
well	O
suffice	O
.	O
</s>
<s>
(	O
The	O
low-order	O
bits	O
of	O
LCGs	B-Algorithm
when	O
m	O
is	O
a	O
power	O
of	O
2	O
should	O
never	O
be	O
relied	O
on	O
for	O
any	O
degree	O
of	O
randomness	O
whatsoever	O
.	O
)	O
</s>
<s>
In	O
particular	O
,	O
any	O
full-cycle	O
LCG	B-Algorithm
,	O
when	O
m	O
is	O
a	O
power	O
of	O
2	O
,	O
will	O
produce	O
alternately	O
odd	O
and	O
even	O
results	O
.	O
</s>
<s>
LCGs	B-Algorithm
should	O
be	O
evaluated	O
very	O
carefully	O
for	O
suitability	O
in	O
non-cryptographic	O
applications	O
where	O
high-quality	O
randomness	O
is	O
critical	O
.	O
</s>
<s>
For	O
Monte	O
Carlo	O
simulations	O
,	O
an	O
LCG	B-Algorithm
must	O
use	O
a	O
modulus	O
greater	O
and	O
preferably	O
much	O
greater	O
than	O
the	O
cube	O
of	O
the	O
number	O
of	O
random	O
samples	O
which	O
are	O
required	O
.	O
</s>
<s>
This	O
means	O
,	O
for	O
example	O
,	O
that	O
a	O
(	O
good	O
)	O
32-bit	O
LCG	B-Algorithm
can	O
be	O
used	O
to	O
obtain	O
about	O
a	O
thousand	O
random	O
numbers	O
;	O
a	O
64-bit	O
LCG	B-Algorithm
is	O
good	O
for	O
about	O
221	O
random	O
samples	O
(	O
a	O
little	O
over	O
two	O
million	O
)	O
,	O
etc	O
.	O
</s>
<s>
For	O
this	O
reason	O
,	O
in	O
practice	O
LCGs	B-Algorithm
are	O
not	O
suitable	O
for	O
large-scale	O
Monte	O
Carlo	O
simulations	O
.	O
</s>
<s>
The	O
following	O
is	O
an	O
implementation	O
of	O
an	O
LCG	B-Algorithm
in	O
Python	B-Language
,	O
in	O
the	O
form	O
of	O
a	O
generator	O
:	O
</s>
<s>
Free	B-Operating_System
Pascal	I-Operating_System
uses	O
a	O
Mersenne	B-Algorithm
Twister	I-Algorithm
as	O
its	O
default	O
pseudo	B-Algorithm
random	I-Algorithm
number	I-Algorithm
generator	I-Algorithm
whereas	O
Delphi	B-Language
uses	O
a	O
LCG	B-Algorithm
.	O
</s>
<s>
Here	O
is	O
a	O
Delphi	B-Language
compatible	O
example	O
in	O
Free	B-Operating_System
Pascal	I-Operating_System
based	O
on	O
the	O
information	O
in	O
the	O
table	O
above	O
.	O
</s>
<s>
Given	O
the	O
same	O
RandSeed	O
value	O
it	O
generates	O
the	O
same	O
sequence	O
of	O
random	O
numbers	O
as	O
Delphi	B-Language
.	O
</s>
<s>
Like	O
all	O
pseudorandom	B-Algorithm
number	I-Algorithm
generators	I-Algorithm
,	O
a	O
LCG	B-Algorithm
needs	O
to	O
store	O
state	O
and	O
alter	O
it	O
each	O
time	O
it	O
generates	O
a	O
new	O
number	O
.	O
</s>
<s>
There	O
are	O
several	O
generators	O
which	O
are	O
linear	B-Algorithm
congruential	I-Algorithm
generators	I-Algorithm
in	O
a	O
different	O
form	O
,	O
and	O
thus	O
the	O
techniques	O
used	O
to	O
analyze	O
LCGs	B-Algorithm
can	O
be	O
applied	O
to	O
them	O
.	O
</s>
<s>
One	O
method	O
of	O
producing	O
a	O
longer	O
period	O
is	O
to	O
sum	O
the	O
outputs	O
of	O
several	O
LCGs	B-Algorithm
of	O
different	O
periods	O
having	O
a	O
large	O
least	O
common	O
multiple	O
;	O
the	O
Wichmann	B-Algorithm
–	I-Algorithm
Hill	I-Algorithm
generator	O
is	O
an	O
example	O
of	O
this	O
form	O
.	O
</s>
<s>
This	O
can	O
be	O
shown	O
to	O
be	O
equivalent	O
to	O
a	O
single	O
LCG	B-Algorithm
with	O
a	O
modulus	O
equal	O
to	O
the	O
product	O
of	O
the	O
component	O
LCG	B-Algorithm
moduli	O
.	O
</s>
<s>
Marsaglia	O
's	O
add-with-carry	O
and	O
subtract-with-borrow	B-Algorithm
PRNGs	O
with	O
a	O
word	O
size	O
of	O
b	O
=	O
2w	O
and	O
lags	O
r	O
and	O
s	O
(	O
r>s	O
)	O
are	O
equivalent	O
to	O
LCGs	B-Algorithm
with	O
a	O
modulus	O
of	O
br±bs±1	O
.	O
</s>
<s>
Multiply-with-carry	B-Algorithm
PRNGs	O
with	O
a	O
multiplier	O
of	O
a	O
are	O
equivalent	O
to	O
LCGs	B-Algorithm
with	O
a	O
large	O
prime	O
modulus	O
of	O
abr−1	O
and	O
a	O
power-of-2	O
multiplier	O
b	O
.	O
</s>
<s>
A	O
permuted	B-Algorithm
congruential	I-Algorithm
generator	I-Algorithm
begins	O
with	O
a	O
power-of-2-modulus	O
LCG	B-Algorithm
and	O
applies	O
an	O
output	O
transformation	O
to	O
eliminate	O
the	O
short	O
period	O
problem	O
in	O
the	O
low-order	O
bits	O
.	O
</s>
<s>
The	O
other	O
widely	O
used	O
primitive	O
for	O
obtaining	O
long-period	O
pseudorandom	B-Algorithm
sequences	I-Algorithm
is	O
the	O
linear-feedback	B-Architecture
shift	I-Architecture
register	I-Architecture
construction	O
,	O
which	O
is	O
based	O
on	O
arithmetic	O
in	O
GF(2 )	O
[x],	O
the	O
polynomial	O
ring	O
over	O
GF(2 )	O
.	O
</s>
<s>
Examples	O
of	O
this	O
family	O
include	O
xorshift	B-Algorithm
generators	O
and	O
the	O
Mersenne	B-Algorithm
twister	I-Algorithm
.	O
</s>
<s>
Lagged	B-Algorithm
Fibonacci	I-Algorithm
generators	I-Algorithm
also	O
fall	O
into	O
this	O
category	O
;	O
although	O
they	O
use	O
arithmetic	O
addition	O
,	O
their	O
period	O
is	O
ensured	O
by	O
an	O
LFSR	B-Architecture
among	O
the	O
least-significant	O
bits	O
.	O
</s>
<s>
It	O
is	O
easy	O
to	O
detect	O
the	O
structure	O
of	O
a	O
linear-feedback	B-Architecture
shift	I-Architecture
register	I-Architecture
with	O
appropriate	O
tests	O
such	O
as	O
the	O
linear	B-Architecture
complexity	O
test	O
implemented	O
in	O
the	O
TestU01	B-Algorithm
suite	O
;	O
a	O
boolean	O
circulant	O
matrix	O
initialized	O
from	O
consecutive	O
bits	O
of	O
an	O
LFSR	B-Architecture
will	O
never	O
have	O
rank	O
greater	O
than	O
the	O
degree	O
of	O
the	O
polynomial	O
.	O
</s>
<s>
Adding	O
a	O
non-linear	O
output	O
mixing	O
function	O
(	O
as	O
in	O
the	O
xoshiro256**	B-Algorithm
and	O
permuted	B-Algorithm
congruential	I-Algorithm
generator	I-Algorithm
constructions	O
)	O
can	O
greatly	O
improve	O
the	O
performance	O
on	O
statistical	O
tests	O
.	O
</s>
<s>
A	O
structure	O
similar	O
to	O
LCGs	B-Algorithm
,	O
but	O
not	O
equivalent	O
,	O
is	O
the	O
multiple-recursive	O
generator	O
:	O
Xn	O
=	O
(	O
a1Xn−1+	O
a2Xn−2+	O
···+	O
akXn−k	O
)	O
modm	O
for	O
k≥2	O
.	O
</s>
<s>
With	O
a	O
prime	O
modulus	O
,	O
this	O
can	O
generate	O
periods	O
up	O
to	O
mk−1	O
,	O
so	O
is	O
a	O
useful	O
extension	O
of	O
the	O
LCG	B-Algorithm
structure	O
to	O
larger	O
periods	O
.	O
</s>
<s>
A	O
powerful	O
technique	O
for	O
generating	O
high-quality	O
pseudorandom	B-Error_Name
numbers	I-Error_Name
is	O
to	O
combine	O
two	O
or	O
more	O
PRNGs	O
of	O
different	O
structure	O
;	O
the	O
sum	O
of	O
an	O
LFSR	B-Architecture
and	O
an	O
LCG	B-Algorithm
(	O
as	O
in	O
the	O
KISS	B-Algorithm
or	O
xorwow	O
constructions	O
)	O
can	O
do	O
very	O
well	O
at	O
some	O
cost	O
in	O
speed	O
.	O
</s>
