Knuth shuffle: Difference between revisions

m
m (→‎version 1, card names: removed OVERFLOW from PRE html tag.)
Line 884:
<lang gap># Return the list L after applying Knuth shuffle. GAP also has the function Shuffle, which does the same.
ShuffleAlt := function(a)
local i, j, n, t;
n := Length(a);
for i in [n, n - 1 .. 2] do
j := Random(1, i);
t := a[i];
a[i] := a[j];
a[j] := t;
od;
return a;
end;
 
# Return a "Permutation" object (a permutation of 1 .. n).
# They are printed in GAP, in cycle decomposition form.
PermShuffle := n -> PermListListPermList([1 .. n], ShuffleShuffleAlt([1 .. n]));
 
ShuffleAlt([1 .. 10]);
Anonymous user