Anonymous user
Knuth shuffle: Difference between revisions
m
→{{header|GAP}}
m (→version 1, card names: removed OVERFLOW from PRE html tag.) |
m (→{{header|GAP}}) |
||
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 ->
ShuffleAlt([1 .. 10]);
|