Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: 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. I think it samples all derangements fairly, but I have not verified this:

my @cols = 0..11; randomly_derange( \@cols ); @nums = map [ @{$_}[@cols] ], @nums; sub randomly_derange { my $arr = shift(); my @i = 0..$#$arr; while ( @i ) { my @swap = map splice( @i, rand( @i ), 1 ), 1, 2; @{ $arr }[ @swap ] = @{ $arr }[ @swap[ 1, 0 ] ]; last if @i == 3; } if ( @i ) { my @j = rand() < 0.5 ? @i[ 1, 2, 0 ] : @i[ 2, 0, 1 ]; @{ $arr }[ @i ] = @{ $arr }[ @j ]; } }
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 3-way derangement (consisting of a 1-rotation, left or right, of some 3-sublist 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 3-way rotation of the three remaining unshuffled elements.

the lowliest monk


In reply to Re: swap columns in a 2-dim array by tlm
in thread swap columns in a 2-dim array by davidj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (11)
    As of 2014-09-01 13:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (10 votes), past polls