RosettaCodeData/Task/Knuth-shuffle/PowerShell/knuth-shuffle-2.ps1

10 lines
300 B
PowerShell
Raw Permalink Normal View History

2026-04-30 12:34:36 -04:00
function shuffle ($a) {
$c = $a.Clone() # make copy to avoid clobbering $a
1..($c.Length - 1) | ForEach-Object {
$i = Get-Random -Minimum $_ -Maximum $c.Length
$c[$_-1],$c[$i] = $c[$i],$c[$_-1]
$c[$_-1] # return newly-shuffled value
}
$c[-1] # last value
}