Knuth shuffle: Difference between revisions

Content added Content deleted
m (→‎version 1, card names: removed OVERFLOW from PRE html tag.)
Line 884: Line 884:
<lang gap># Return the list L after applying Knuth shuffle. GAP also has the function Shuffle, which does the same.
<lang gap># Return the list L after applying Knuth shuffle. GAP also has the function Shuffle, which does the same.
ShuffleAlt := function(a)
ShuffleAlt := function(a)
local i, j, n, t;
local i, j, n, t;
n := Length(a);
n := Length(a);
for i in [n, n - 1 .. 2] do
for i in [n, n - 1 .. 2] do
j := Random(1, i);
j := Random(1, i);
t := a[i];
t := a[i];
a[i] := a[j];
a[i] := a[j];
a[j] := t;
a[j] := t;
od;
od;
return a;
return a;
end;
end;


# Return a "Permutation" object (a permutation of 1 .. n).
# Return a "Permutation" object (a permutation of 1 .. n).
# They are printed in GAP, in cycle decomposition form.
# They are printed in GAP, in cycle decomposition form.
PermShuffle := n -> PermListList([1 .. n], Shuffle([1 .. n]));
PermShuffle := n -> PermList(ShuffleAlt([1 .. n]));


ShuffleAlt([1 .. 10]);
ShuffleAlt([1 .. 10]);