<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
multiply-with-carry	B-Algorithm
(	O
MWC	O
)	O
is	O
a	O
method	O
invented	O
by	O
George	O
Marsaglia	O
for	O
generating	O
sequences	O
of	O
random	O
integers	O
based	O
on	O
an	O
initial	O
set	O
from	O
two	O
to	O
many	O
thousands	O
of	O
randomly	O
chosen	O
seed	O
values	O
.	O
</s>
<s>
As	O
with	O
all	O
pseudorandom	B-Algorithm
number	I-Algorithm
generators	I-Algorithm
,	O
the	O
resulting	O
sequences	O
are	O
functions	O
of	O
the	O
supplied	O
seed	O
values	O
.	O
</s>
<s>
An	O
MWC	O
generator	O
is	O
a	O
special	O
form	O
of	O
Lehmer	B-Algorithm
random	I-Algorithm
number	I-Algorithm
generator	I-Algorithm
which	O
allows	O
efficient	O
implementation	O
of	O
a	O
prime	O
modulus	O
much	O
larger	O
than	O
the	O
machine	O
word	O
size	O
.	O
</s>
<s>
Normal	O
Lehmer	B-Algorithm
generator	I-Algorithm
implementations	O
choose	O
a	O
modulus	O
close	O
to	O
the	O
machine	O
word	O
size	O
.	O
</s>
<s>
This	O
may	O
vary	O
from	O
for	O
a	O
microcontroller	B-Architecture
to	O
.	O
</s>
<s>
The	O
state	O
transformation	O
function	O
of	O
an	O
MWC	O
generator	O
is	O
one	O
step	O
of	O
Montgomery	B-Algorithm
reduction	I-Algorithm
modulo	O
p	O
.	O
The	O
state	O
is	O
a	O
large	O
integer	O
with	O
most	O
significant	O
word	O
cn−1	O
and	O
least	O
significant	O
word	O
xn−r	O
.	O
</s>
<s>
Thus	O
,	O
a	O
multiply-with-carry	B-Algorithm
generator	O
is	O
a	O
Lehmer	B-Algorithm
generator	I-Algorithm
with	O
modulus	O
p	O
and	O
multiplier	O
b−1	O
(	O
mod	O
p	O
)	O
.	O
</s>
<s>
Couture	O
and	O
L'Ecuyer	O
have	O
proved	O
the	O
surprising	O
result	O
that	O
the	O
lattice	O
associated	O
with	O
a	O
multiply-with-carry	B-Algorithm
generator	O
is	O
very	O
close	O
to	O
the	O
lattice	O
associated	O
with	O
the	O
Lehmer	B-Algorithm
generator	I-Algorithm
it	O
simulates	O
.	O
</s>
<s>
Thus	O
,	O
the	O
mathematical	O
techniques	O
developed	O
for	O
Lehmer	B-Algorithm
generators	I-Algorithm
(	O
such	O
as	O
the	O
spectral	B-Algorithm
test	I-Algorithm
)	O
can	O
be	O
applied	O
to	O
multiply-with-carry	B-Algorithm
generators	O
.	O
</s>
<s>
where	O
c	B-Language
is	O
a	O
constant	O
.	O
</s>
<s>
If	O
a	O
≡	O
1	O
(	O
mod	O
4	O
)	O
and	O
c	B-Language
is	O
odd	O
,	O
the	O
resulting	O
base-232	O
congruential	O
sequence	O
will	O
have	O
period	O
232	O
.	O
</s>
<s>
However	O
,	O
many	O
microprocessors	B-Architecture
can	O
compute	O
a	O
full	O
64-bit	O
product	O
in	O
almost	O
the	O
same	O
time	O
as	O
the	O
low	O
32	O
bits	O
.	O
</s>
<s>
A	O
lag-1	O
multiply-with-carry	B-Algorithm
generator	O
allows	O
us	O
to	O
make	O
the	O
period	O
nearly	O
263	O
by	O
using	O
almost	O
the	O
same	O
computer	O
operations	O
,	O
except	O
that	O
the	O
top	O
half	O
of	O
the	O
64-bit	O
product	O
is	O
not	O
ignored	O
after	O
the	O
product	O
is	O
computed	O
.	O
</s>
<s>
Instead	O
,	O
a	O
64-bit	O
sum	O
is	O
computed	O
,	O
and	O
the	O
top	O
half	O
is	O
used	O
as	O
a	O
new	O
carry	O
value	O
c	B-Language
rather	O
than	O
the	O
fixed	O
additive	O
constant	O
of	O
the	O
standard	O
congruential	O
sequence	O
:	O
</s>
<s>
Compute	O
ax+c	O
in	O
64	O
bits	O
,	O
then	O
use	O
the	O
top	O
half	O
as	O
the	O
new	O
c	B-Language
,	O
and	O
bottom	O
half	O
as	O
the	O
new	O
x	O
.	O
</s>
<s>
each	O
pair	O
of	O
input	O
values	O
x	O
,	O
c	B-Language
is	O
converted	O
to	O
a	O
new	O
pair	O
,	O
</s>
<s>
In	O
such	O
a	O
case	O
,	O
for	O
b	O
=	O
232	O
and	O
r	O
=	O
1	O
,	O
the	O
period	O
will	O
be	O
abr/2	O
−	O
1	O
,	O
approaching	O
263	O
,	O
which	O
in	O
practice	O
may	O
be	O
an	O
acceptably	O
large	O
subset	O
of	O
the	O
number	O
of	O
possible	O
32-bit	O
pairs	O
(	O
x	O
,	O
c	B-Language
)	O
.	O
</s>
<s>
The	O
output	O
of	O
a	O
multiply-with-carry	B-Algorithm
generator	O
is	O
equivalent	O
to	O
the	O
radix-b	O
expansion	O
of	O
a	O
fraction	O
with	O
denominator	O
p	O
=	O
abr	O
−	O
1	O
.	O
</s>
<s>
is	O
exactly	O
the	O
Lehmer	B-Algorithm
random	I-Algorithm
number	I-Algorithm
generator	I-Algorithm
output	O
sequence	O
yn	O
=	O
ayn	O
−	O
1	O
mod	O
(	O
ab	O
−	O
1	O
)	O
,	O
</s>
<s>
The	O
modified	O
procedure	O
is	O
called	O
complementary-multiply-with-carry	O
(	O
CMWC	O
)	O
,	O
and	O
the	O
setup	O
is	O
the	O
same	O
as	O
that	O
for	O
lag-r	O
MWC	O
:	O
multiplier	O
a	O
,	O
base	O
b	O
,	O
and	O
r	O
+	O
1	O
seeds	O
,	O
</s>
<s>
The	O
modification	O
is	O
to	O
the	O
generation	O
of	O
a	O
new	O
pair	O
(	O
x	O
,	O
c	B-Language
)	O
.	O
</s>
<s>
Fortunately	O
,	O
the	O
Montgomery	B-Algorithm
reduction	I-Algorithm
algorithm	O
permits	O
other	O
moduli	O
,	O
as	O
long	O
as	O
they	O
are	O
relatively	O
prime	O
to	O
the	O
base	O
b	O
,	O
and	O
this	O
can	O
be	O
applied	O
to	O
permit	O
a	O
modulus	O
of	O
the	O
form	O
arbr−a0	O
,	O
for	O
a	O
wide	O
range	O
of	O
values	O
a0	O
.	O
</s>
<s>
Goresky	O
and	O
Klapper	O
developed	O
the	O
theory	O
of	O
these	O
generalized	O
multiply-with-carry	B-Algorithm
generators	O
,	O
proving	O
,	O
in	O
particular	O
,	O
that	O
choosing	O
a	O
negative	O
a0	O
and	O
ar	O
–	O
a0	O
<	O
b	O
the	O
carry	O
value	O
is	O
always	O
smaller	O
than	O
b	O
,	O
making	O
the	O
implementation	O
efficient	O
.	O
</s>
<s>
The	O
following	O
is	O
an	O
implementation	O
of	O
the	O
CMWC	O
algorithm	O
in	O
the	O
C	B-Language
programming	I-Language
language	I-Language
.	O
</s>
<s>
In	O
libtcod	O
,	O
CMWC4096	O
replaced	O
MT19937	B-Algorithm
as	O
the	O
default	O
PRNG	O
.	O
</s>
