As far as I can tell, your code is a "recoding" of the Fisher-Yates shuffle:
sub fisher_yates_shuffle { my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i]; } }

And permuting your version:

for (0..($#arr-1)) { $n = int (rand() * ($#arr - $_ + 1)) + $_; $t = $arr[$n]; $arr[$n] = $arr[$_]; $arr[$_] = $t; }

for (0..($#arr-1)) { $n = int (rand() * ($#arr - $_ + 1)) + $_;
$t = $arr[$n]; $arr[$n] = $arr[$_]; $arr[$_] = $t;
}

for (0..($#arr-1)) { $n = int (rand() * ($#arr - $_ + 1)) + $_;
@$arr[$_,$n] = @$arr[$n,$_];
}

for (0..($#arr-1)) { $n = int (rand() * ($#arr - $_ + 1)) + $_;
# next if $_ == $n;
@$arr[$_,$n] = @$arr[$n,$_]; }

The remaining two lines are functionally equivalent:

for (0..($#arr-1)) { # Your Version $n = int (rand() * ($#arr - $_ + 1)) + $_; for ($i = @$array; --$i; ) { # Fisher-Yates my $j = int rand ($i+1);

Frankly, I prefer your formulation (with the addition of the next if line) as it doesn't use an incomplete for loop, but the codes are equivalent.

UPDATE: For the benefit of the astute who realized that the remaining two lines weren't entirely equivalent:

for (0..($#arr-1)) { $n = int (
rand() * ($#arr - $_ + 1)
) + $_; # next if $_ == $n; @$arr[$_,$n] = @$arr[$n,$_]; }

for (0..($#arr-1)) { $n = int (
rand($#arr - $_ + 1)
) + $_; # next if $_ == $n; @$arr[$_,$n] = @$arr[$n,$_]; }

for (0..($#arr-1)) { $n = int (rand($#arr - $_ + 1))
+ $_
; # next if $_ == $n; @$arr[$_,$n] = @$arr[$n,$_]; }

Fisher-Yates traverses down the array, whereas this version traverses up the array (due to the appended + $_). Remove the addition and the codes are equivalent; leave it in and the codes are functionally equivalent.


In reply to Re^2: unsorted list by eibwen
in thread unsorted list by thekestrel

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.