Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Random Derangement Of An Array

by Corion (Pope)
on Jul 05, 2008 at 21:44 UTC ( #695751=note: print w/replies, xml ) Need Help??

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)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://695751]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2021-02-28 04:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found