<s>
The	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
is	O
an	O
algorithm	O
for	O
generating	O
a	O
random	B-Algorithm
permutation	I-Algorithm
of	O
a	O
finite	O
sequence	O
—	O
in	O
plain	O
terms	O
,	O
the	O
algorithm	O
shuffles	O
the	O
sequence	O
.	O
</s>
<s>
The	O
modern	O
version	O
of	O
the	O
algorithm	O
is	O
efficient	O
:	O
it	O
takes	O
time	O
proportional	O
to	O
the	O
number	O
of	O
items	O
being	O
shuffled	O
and	O
shuffles	O
them	O
in	B-Algorithm
place	I-Algorithm
.	O
</s>
<s>
The	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
is	O
named	O
after	O
Ronald	O
Fisher	O
and	O
Frank	O
Yates	O
,	O
who	O
first	O
described	O
it	O
,	O
and	O
is	O
also	O
known	O
as	O
the	O
Knuth	B-Algorithm
shuffle	I-Algorithm
after	O
Donald	O
Knuth	O
.	O
</s>
<s>
A	O
variant	O
of	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
,	O
known	O
as	O
Sattolo	O
's	O
algorithm	O
,	O
may	O
be	O
used	O
to	O
generate	O
random	O
cyclic	B-Algorithm
permutations	I-Algorithm
of	O
length	O
n	O
instead	O
of	O
random	B-Algorithm
permutations	I-Algorithm
.	O
</s>
<s>
The	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
,	O
in	O
its	O
original	O
form	O
,	O
was	O
described	O
in	O
1938	O
by	O
Ronald	O
Fisher	O
and	O
Frank	O
Yates	O
in	O
their	O
book	O
Statistical	O
tables	O
for	O
biological	O
,	O
agricultural	O
and	O
medical	O
research	O
.	O
</s>
<s>
The	O
basic	O
method	O
given	O
for	O
generating	O
a	O
random	B-Algorithm
permutation	I-Algorithm
of	O
the	O
numbers	O
1	O
through	O
N	O
goes	O
as	O
follows	O
:	O
</s>
<s>
The	O
sequence	O
of	O
numbers	O
written	O
down	O
in	O
step	O
3	O
is	O
now	O
a	O
random	B-Algorithm
permutation	I-Algorithm
of	O
the	O
original	O
numbers	O
.	O
</s>
<s>
The	O
modern	O
version	O
of	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
,	O
designed	O
for	O
computer	O
use	O
,	O
was	O
introduced	O
by	O
Richard	O
Durstenfeld	O
in	O
1964	O
and	O
popularized	O
by	O
Donald	O
E	O
.	O
Knuth	O
in	O
The	B-General_Concept
Art	I-General_Concept
of	I-General_Concept
Computer	I-General_Concept
Programming	I-General_Concept
as	O
"	O
Algorithm	B-Algorithm
P	I-Algorithm
(	O
Shuffling	O
)	O
"	O
.	O
</s>
<s>
Neither	O
Durstenfeld	O
's	O
article	O
nor	O
Knuth	O
's	O
first	O
edition	O
of	O
The	B-General_Concept
Art	I-General_Concept
of	I-General_Concept
Computer	I-General_Concept
Programming	I-General_Concept
acknowledged	O
the	O
work	O
of	O
Fisher	O
and	O
Yates	O
;	O
they	O
may	O
not	O
have	O
been	O
aware	O
of	O
it	O
.	O
</s>
<s>
Subsequent	O
editions	O
of	O
Knuth	O
's	O
The	B-General_Concept
Art	I-General_Concept
of	I-General_Concept
Computer	I-General_Concept
Programming	I-General_Concept
mention	O
Fisher	O
and	O
Yates	O
 '	O
contribution	O
.	O
</s>
<s>
This	O
change	O
gives	O
the	O
following	O
algorithm	O
(	O
for	O
a	O
zero-based	O
array	B-Data_Structure
)	O
.	O
</s>
<s>
--	O
To	O
shuffle	O
an	O
array	B-Data_Structure
a	O
of	O
n	O
elements	O
(	O
indices	O
0	O
..	O
n	O
)	O
:	O
</s>
<s>
An	O
equivalent	O
version	O
which	O
shuffles	O
the	O
array	B-Data_Structure
in	O
the	O
opposite	O
direction	O
(	O
from	O
lowest	O
index	O
to	O
highest	O
)	O
is	O
:	O
</s>
<s>
--	O
To	O
shuffle	O
an	O
array	B-Data_Structure
a	O
of	O
n	O
elements	O
(	O
indices	O
0	O
..	O
n	O
)	O
:	O
</s>
<s>
A	O
new	O
alternative	O
to	O
Fisher-Yates	B-Algorithm
,	O
which	O
does	O
not	O
use	O
any	O
array	B-Data_Structure
memory	O
operations	O
,	O
is	O
the	O
use	O
a	O
Pseudo	B-Error_Name
Random	I-Error_Name
Index	O
Generator	O
(	O
PRIG	O
)	O
function	O
algorithm	O
.	O
</s>
<s>
The	O
next	O
random	O
number	O
we	O
roll	O
is	O
from	O
1	O
to	O
6	O
,	O
and	O
just	O
happens	O
to	O
be	O
6	O
,	O
which	O
means	O
we	O
leave	O
the	O
6th	O
letter	O
in	O
the	O
list	O
(	O
which	O
,	O
after	O
the	O
swap	O
above	O
,	O
is	O
now	O
letter	O
H	O
)	O
in	B-Algorithm
place	I-Algorithm
and	O
just	O
move	O
to	O
the	O
next	O
step	O
.	O
</s>
<s>
The	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
,	O
as	O
implemented	O
by	O
Durstenfeld	O
,	O
is	O
an	O
in-place	B-Algorithm
shuffle	O
.	O
</s>
<s>
That	O
is	O
,	O
given	O
a	O
preinitialized	O
array	B-Data_Structure
,	O
it	O
shuffles	O
the	O
elements	O
of	O
the	O
array	B-Data_Structure
in	B-Algorithm
place	I-Algorithm
,	O
rather	O
than	O
producing	O
a	O
shuffled	O
copy	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
This	O
can	O
be	O
an	O
advantage	O
if	O
the	O
array	B-Data_Structure
to	O
be	O
shuffled	O
is	O
large	O
.	O
</s>
<s>
To	O
simultaneously	O
initialize	O
and	O
shuffle	O
an	O
array	B-Data_Structure
,	O
a	O
bit	O
more	O
efficiency	O
can	O
be	O
attained	O
by	O
doing	O
an	O
"	O
inside-out	O
"	O
version	O
of	O
the	O
shuffle	O
.	O
</s>
<s>
In	O
this	O
version	O
,	O
one	O
successively	O
places	O
element	O
number	O
i	O
into	O
a	O
random	O
position	O
among	O
the	O
first	O
i	O
positions	O
in	O
the	O
array	B-Data_Structure
,	O
after	O
moving	O
the	O
element	O
previously	O
occupying	O
that	O
position	O
to	O
position	O
i	O
.	O
</s>
<s>
To	O
initialize	O
an	O
array	B-Data_Structure
a	O
of	O
n	O
elements	O
to	O
a	O
randomly	O
shuffled	O
copy	O
of	O
source	O
,	O
both	O
0-based	O
:	O
</s>
<s>
The	O
inside-out	O
shuffle	O
can	O
be	O
seen	O
to	O
be	O
correct	O
by	B-Algorithm
induction	I-Algorithm
.	O
</s>
<s>
The	O
condition	O
that	O
checks	O
if	O
j	O
≠	O
i	O
may	O
be	O
omitted	O
in	O
languages	O
that	O
have	O
no	O
problems	O
accessing	O
uninitialized	O
array	B-Data_Structure
values	O
.	O
</s>
<s>
Below	O
the	O
array	B-Data_Structure
a	O
is	O
built	O
iteratively	O
starting	O
from	O
empty	O
,	O
and	O
a.length	O
represents	O
the	O
current	O
number	O
of	O
elements	O
seen	O
.	O
</s>
<s>
To	O
initialize	O
an	O
empty	O
array	B-Data_Structure
a	O
to	O
a	O
randomly	O
shuffled	O
copy	O
of	O
source	O
whose	O
length	O
is	O
not	O
known	O
:	O
</s>
<s>
This	O
simple	O
change	O
modifies	O
the	O
algorithm	O
so	O
that	O
the	O
resulting	O
permutation	O
always	O
consists	O
of	O
a	O
single	O
cycle	B-Algorithm
.	O
</s>
<s>
In	O
fact	O
,	O
as	O
described	O
below	O
,	O
it	O
is	O
quite	O
easy	O
to	O
accidentally	O
implement	O
Sattolo	O
's	O
algorithm	O
when	O
the	O
ordinary	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
is	O
intended	O
.	O
</s>
<s>
The	O
fact	O
that	O
Sattolo	O
's	O
algorithm	O
always	O
produces	O
a	O
cycle	B-Algorithm
of	O
length	O
n	O
can	O
be	O
shown	O
by	B-Algorithm
induction	I-Algorithm
.	O
</s>
<s>
Assume	O
by	B-Algorithm
induction	I-Algorithm
that	O
after	O
the	O
initial	O
iteration	O
of	O
the	O
loop	O
,	O
the	O
remaining	O
iterations	O
permute	O
the	O
first	O
n−1	O
elements	O
according	O
to	O
a	O
cycle	B-Algorithm
of	O
length	O
n−1	O
(	O
those	O
remaining	O
iterations	O
are	O
just	O
Sattolo	O
's	O
algorithm	O
applied	O
to	O
those	O
first	O
n−1	O
elements	O
)	O
.	O
</s>
<s>
different	O
permutations	O
so	O
produced	O
precisely	O
exhaust	O
the	O
set	O
of	O
cycles	O
of	O
length	O
n	O
:	O
each	O
such	O
cycle	B-Algorithm
has	O
a	O
unique	O
cycle	B-Algorithm
notation	I-Algorithm
with	O
the	O
value	O
n	O
in	O
the	O
final	O
position	O
,	O
which	O
allows	O
for	O
(	O
n−1	O
)	O
!	O
</s>
<s>
permutations	O
of	O
the	O
remaining	O
values	O
to	O
fill	O
the	O
other	O
positions	O
of	O
the	O
cycle	B-Algorithm
notation	I-Algorithm
.	O
</s>
<s>
A	O
sample	O
implementation	O
of	O
Sattolo	O
's	O
algorithm	O
in	O
Python	B-Language
is	O
:	O
</s>
<s>
The	O
asymptotic	O
time	O
and	O
space	O
complexity	O
of	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
are	O
optimal	O
.	O
</s>
<s>
The	O
sorting	O
method	O
has	O
the	O
same	O
asymptotic	O
time	O
complexity	O
as	O
Fisher	O
–	O
Yates	O
:	O
although	O
general	O
sorting	O
is	O
O(nlogn )	O
,	O
numbers	O
are	O
efficiently	O
sorted	O
using	O
Radix	B-Algorithm
sort	I-Algorithm
in	O
O(n )	O
time	O
.	O
</s>
<s>
Like	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
,	O
the	O
sorting	O
method	O
produces	O
unbiased	O
results	O
.	O
</s>
<s>
Additionally	O
,	O
this	O
method	O
requires	O
asymptotically	O
larger	O
space	O
:	O
O(n )	O
additional	O
storage	O
space	O
for	O
the	O
random	O
numbers	O
,	O
versus	O
O(1 )	O
space	O
for	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
.	O
</s>
<s>
Finally	O
,	O
we	O
note	O
that	O
the	O
sorting	O
method	O
has	O
a	O
simple	O
parallel	B-Operating_System
implementation	O
,	O
unlike	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
,	O
which	O
is	O
sequential	O
.	O
</s>
<s>
For	O
instance	O
suppose	O
quicksort	B-Algorithm
is	O
used	O
as	O
sorting	O
algorithm	O
,	O
with	O
a	O
fixed	O
element	O
selected	O
as	O
first	O
pivot	B-Algorithm
element	I-Algorithm
.	O
</s>
<s>
The	O
algorithm	O
starts	O
comparing	O
the	O
pivot	O
with	O
all	O
other	O
elements	O
to	O
separate	O
them	O
into	O
those	O
less	O
and	O
those	O
greater	O
than	O
it	O
,	O
and	O
the	O
relative	O
sizes	O
of	O
those	O
groups	O
will	O
determine	O
the	O
final	O
place	O
of	O
the	O
pivot	B-Algorithm
element	I-Algorithm
.	O
</s>
<s>
For	O
a	O
uniformly	O
distributed	O
random	B-Algorithm
permutation	I-Algorithm
,	O
each	O
possible	O
final	O
position	O
should	O
be	O
equally	O
likely	O
for	O
the	O
pivot	B-Algorithm
element	I-Algorithm
,	O
but	O
if	O
each	O
of	O
the	O
initial	O
comparisons	O
returns	O
"	O
less	O
"	O
or	O
"	O
greater	O
"	O
with	O
equal	O
probability	O
,	O
then	O
that	O
position	O
will	O
have	O
a	O
binomial	O
distribution	O
for	O
p	O
=	O
1/2	O
,	O
which	O
gives	O
positions	O
near	O
the	O
middle	O
of	O
the	O
sequence	O
with	O
a	O
much	O
higher	O
probability	O
for	O
than	O
positions	O
near	O
the	O
ends	O
.	O
</s>
<s>
Randomized	O
comparison	O
functions	O
applied	O
to	O
other	O
sorting	O
methods	O
like	O
merge	B-Algorithm
sort	I-Algorithm
may	O
produce	O
results	O
that	O
appear	O
more	O
uniform	O
,	O
but	O
are	O
not	O
quite	O
so	O
either	O
,	O
since	O
merging	O
two	O
sequences	O
by	O
repeatedly	O
choosing	O
one	O
of	O
them	O
with	O
equal	O
probability	O
(	O
until	O
the	O
choice	O
is	O
forced	O
by	O
the	O
exhaustion	O
of	O
one	O
sequence	O
)	O
does	O
not	O
produce	O
results	O
with	O
a	O
uniform	O
distribution	O
;	O
instead	O
the	O
probability	O
to	O
choose	O
a	O
sequence	O
should	O
be	O
proportional	O
to	O
the	O
number	O
of	O
elements	O
left	O
in	O
it	O
.	O
</s>
<s>
For	O
instance	O
the	O
fact	O
that	O
any	O
element	O
should	O
compare	O
equal	O
to	O
itself	O
allows	O
using	O
them	O
as	O
sentinel	B-Data_Structure
value	I-Data_Structure
for	O
efficiency	O
reasons	O
,	O
and	O
if	O
this	O
is	O
the	O
case	O
,	O
a	O
random	O
comparison	O
function	O
would	O
break	O
the	O
sorting	O
algorithm	O
.	O
</s>
<s>
Care	O
must	O
be	O
taken	O
when	O
implementing	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
,	O
both	O
in	O
the	O
implementation	O
of	O
the	O
algorithm	O
itself	O
and	O
in	O
the	O
generation	O
of	O
the	O
random	O
numbers	O
it	O
is	O
built	O
on	O
,	O
otherwise	O
the	O
results	O
may	O
show	O
detectable	O
bias	O
.	O
</s>
<s>
A	O
common	O
error	O
when	O
implementing	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
is	O
to	O
pick	O
the	O
random	O
numbers	O
from	O
the	O
wrong	O
range	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
common	O
off-by-one	B-Error_Name
error	I-Error_Name
would	O
be	O
choosing	O
the	O
index	O
j	O
of	O
the	O
entry	O
to	O
swap	O
in	O
the	O
example	O
above	O
to	O
be	O
always	O
strictly	O
less	O
than	O
the	O
index	O
i	O
of	O
the	O
entry	O
it	O
will	O
be	O
swapped	O
with	O
.	O
</s>
<s>
This	O
turns	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
into	O
Sattolo	O
's	O
algorithm	O
,	O
which	O
produces	O
only	O
permutations	O
consisting	O
of	O
a	O
single	O
cycle	B-Algorithm
involving	O
all	O
elements	O
:	O
in	O
particular	O
,	O
with	O
this	O
modification	O
,	O
no	O
element	O
of	O
the	O
array	B-Data_Structure
can	O
ever	O
end	O
up	O
in	O
its	O
original	O
position	O
.	O
</s>
<s>
Similarly	O
,	O
always	O
selecting	O
j	O
from	O
the	O
entire	O
range	O
of	O
valid	O
array	B-Data_Structure
indices	O
on	O
every	O
iteration	O
also	O
produces	O
a	O
result	O
which	O
is	O
biased	O
,	O
albeit	O
less	O
obviously	O
so	O
.	O
</s>
<s>
possible	O
permutations	O
of	O
an	O
n-element	O
array	B-Data_Structure
.	O
</s>
<s>
As	O
a	O
concrete	O
example	O
of	O
this	O
bias	O
,	O
observe	O
the	O
distribution	O
of	O
possible	O
outcomes	O
of	O
shuffling	O
a	O
three-element	O
array	B-Data_Structure
[	O
1	O
,	O
2	O
,	O
3 ]	O
.	O
</s>
<s>
There	O
are	O
6	O
possible	O
permutations	O
of	O
this	O
array	B-Data_Structure
(	O
3	O
!	O
</s>
<s>
Doing	O
a	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
involves	O
picking	O
uniformly	O
distributed	O
random	O
integers	O
from	O
various	O
ranges	O
.	O
</s>
<s>
Most	O
random	O
number	O
generators	O
,	O
however	O
—	O
whether	O
true	O
or	O
pseudorandom	B-Error_Name
—	O
will	O
only	O
directly	O
provide	O
numbers	O
in	O
a	O
fixed	O
range	O
from	O
0	O
to	O
RAND_MAX	O
,	O
and	O
in	O
some	O
libraries	O
,	O
RAND_MAX	O
may	O
be	O
as	O
low	O
as	O
32767	O
.	O
</s>
<s>
However	O
,	O
the	O
need	O
in	O
a	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
to	O
generate	O
random	O
numbers	O
in	O
every	O
range	O
from	O
0	O
–	O
1	O
to	O
0	O
–	O
n	O
almost	O
guarantees	O
that	O
some	O
of	O
these	O
ranges	O
will	O
not	O
evenly	O
divide	O
the	O
natural	O
range	O
of	O
the	O
random	O
number	O
generator	O
.	O
</s>
<s>
A	O
related	O
problem	O
occurs	O
with	O
implementations	O
that	O
first	O
generate	O
a	O
random	O
floating-point	B-Algorithm
number	I-Algorithm
—	O
usually	O
in	O
the	O
range	O
[0,1]—and	O
then	O
multiply	O
it	O
by	O
the	O
size	O
of	O
the	O
desired	O
range	O
and	O
round	O
down	O
.	O
</s>
<s>
The	O
problem	O
here	O
is	O
that	O
random	O
floating-point	B-Algorithm
numbers	I-Algorithm
,	O
however	O
carefully	O
generated	O
,	O
always	O
have	O
only	O
finite	O
precision	O
.	O
</s>
<s>
This	O
means	O
that	O
there	O
are	O
only	O
a	O
finite	O
number	O
of	O
possible	O
floating	B-Algorithm
point	I-Algorithm
values	I-Algorithm
in	O
any	O
given	O
range	O
,	O
and	O
if	O
the	O
range	O
is	O
divided	O
into	O
a	O
number	O
of	O
segments	O
that	O
does	O
n't	O
divide	O
this	O
number	O
evenly	O
,	O
some	O
segments	O
will	O
end	O
up	O
with	O
more	O
possible	O
values	O
than	O
others	O
.	O
</s>
<s>
An	O
additional	O
problem	O
occurs	O
when	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
is	O
used	O
with	O
a	O
pseudorandom	B-Algorithm
number	I-Algorithm
generator	I-Algorithm
or	O
PRNG	O
:	O
as	O
the	O
sequence	O
of	O
numbers	O
output	O
by	O
such	O
a	O
generator	O
is	O
entirely	O
determined	O
by	O
its	O
internal	O
state	O
at	O
the	O
start	O
of	O
a	O
sequence	O
,	O
a	O
shuffle	O
driven	O
by	O
such	O
a	O
generator	O
cannot	O
possibly	O
produce	O
more	O
distinct	O
permutations	O
than	O
the	O
generator	O
has	O
distinct	O
possible	O
states	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
built-in	O
pseudorandom	B-Algorithm
number	I-Algorithm
generator	I-Algorithm
provided	O
by	O
many	O
programming	O
languages	O
and/or	O
libraries	O
may	O
often	O
have	O
only	O
32	O
bits	O
of	O
internal	O
state	O
,	O
which	O
means	O
it	O
can	O
only	O
produce	O
232	O
different	O
sequences	O
of	O
numbers	O
.	O
</s>
<s>
No	O
pseudorandom	B-Algorithm
number	I-Algorithm
generator	I-Algorithm
can	O
produce	O
more	O
distinct	O
sequences	O
,	O
starting	O
from	O
the	O
point	O
of	O
initialization	O
,	O
than	O
there	O
are	O
distinct	O
seed	O
values	O
it	O
may	O
be	O
initialized	O
with	O
.	O
</s>
<s>
A	O
further	O
problem	O
occurs	O
when	O
a	O
simple	O
linear	B-Algorithm
congruential	I-Algorithm
PRNG	O
is	O
used	O
with	O
the	O
divide-and-take-remainder	O
method	O
of	O
range	O
reduction	O
described	O
above	O
.	O
</s>
<s>
The	O
problem	O
here	O
is	O
that	O
the	O
low-order	O
bits	O
of	O
a	O
linear	B-Algorithm
congruential	I-Algorithm
PRNG	O
with	O
modulo	O
2e	O
are	O
less	O
random	O
than	O
the	O
high-order	O
ones	O
:	O
the	O
low	O
n	O
bits	O
of	O
the	O
generator	O
themselves	O
have	O
a	O
period	O
of	O
at	O
most	O
2n	O
.	O
</s>
<s>
Different	O
rules	O
apply	O
if	O
the	O
LCG	B-Algorithm
has	O
prime	O
modulo	O
,	O
but	O
such	O
generators	O
are	O
uncommon	O
.	O
</s>
