Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:
All,
I need to derange the values of an array randomly (for some definition of random). I could use tye's very nice Derangements iterator and call it a random number of times, but that seems wasteful. I could also use List::Util's shuffle() and continue to swap and elements that remain in their original positions until all are swapped - but that's seems clumsy.
I need to derange the values of an array randomly (for some definition of random). I could use tye's very nice Derangements iterator and call it a random number of times, but that seems wasteful. I could also use List::Util's shuffle() and continue to swap and elements that remain in their original positions until all are swapped - but that's seems clumsy.
Anyone have a good algorithm for generating a random derangement?
Cheers - L~R
Back to
Seekers of Perl Wisdom