http://qs321.pair.com?node_id=695751


in reply to Random Derangement Of An Array

All I'm coming up with are trivial methods:

You can just shift each item by n, where @array > n > 1:

my $shift = rand $#array; @array = map { $array[ ($_ + $shift) % @array ]} 0..$#array;

Also, reverse is a derangement for even-sized lists, and for odd-sized lists, I guess you can prepend the middle item and reverse the rest:

my $shift = rand $#array; if (@array % 2 == 0) { @array = map { $array[ ($_ + $shift) % @array} reverse 0..$#array; } else { my $middle = int(@array /2); my @middle = splice @array, $middle, 1; @array = @middle, map { $array[ ($_ + $shift) % @array} reverse 0. +.$#array; };

... but all of these derangements are pretty predictable (that is, from the position of one item, you can infer the positions of all other items with two or three guesses)