Syntactic Confectionery Delight  
PerlMonks 
Re: swap columns in a 2dim arrayby tlm (Prior) 
on Jun 17, 2005 at 10:47 UTC ( #467647=note: print w/ replies, xml )  Need Help?? 
Update: Like the saying goes: problems worthy of attack prove their worth by biting back. Immediately after I posted the scheme below I realized that it too fails to sample all the derangements. The decomposition of a derangement I give in the description of the algorithms is incorrect. For example, it does not describe the derangement 1,2,3,0 of 0,1,2,3. The moral of the story: do the math before doing the coding :) . I am beginning to see why my (admittedly very superficial) online searches for a derangement sampling algorithm turned up nothing cleverer than a rejection method. OK, below I give a first pass at a function to generate a random derangement. Even if it is correct, I am sure that there is plenty of room for optimizing randomly_derange. The idea behind it is this. Every derangement of a list having an even number of elements can be represented as a series of pairwise swaps. When the list has an odd number of elements, every derangement can be represented as a series of pairwise swaps, plus a 3way derangement (consisting of a 1rotation, left or right, of some 3sublist of the original list). randomly_derange picks random swaps uniformly. When the list has an even number of elements, that's all there is to it. When the list has an odd number of elements, then at the end it performs one of two possible 3way rotation of the three remaining unshuffled elements. the lowliest monk
In Section
Seekers of Perl Wisdom

