The shuffle loop will never include the last element of the array. ... rand($i + 1) will never return the value of $i.

BramVanOosterhout: Have you checked these assertions of How do I shuffle an array??

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "sub fisher_yates_shuffle_while_loop { my $array = shift; my $i = @$array; while ( --$i ) { my $j = int rand( $i+1 ); @$array[$i,$j] = @$array[$j,$i]; } } ;; my @ra = (1 .. 9); fisher_yates_shuffle_while_loop(\@ra); dd \@ra; @ra = (1, 2); fisher_yates_shuffle_while_loop(\@ra); dd \@ra; @ra = (1); fisher_yates_shuffle_while_loop(\@ra); dd \@ra; ;; ;; sub fisher_yates_shuffle_for_loop { my $array = shift; my $i = @$array; for ( my $i = @$array; $i; $i-- ) { my $j = int rand( $i+1 ); @$array[$i,$j] = @$array[$j,$i]; } } ;; @ra = (1 .. 9); fisher_yates_shuffle_for_loop(\@ra); dd \@ra; @ra = (1, 2); fisher_yates_shuffle_for_loop(\@ra); dd \@ra; @ra = (1); fisher_yates_shuffle_for_loop(\@ra); dd \@ra; ;; ;; for (0 .. 9) { printf '%d ', int rand(3 + 1); } " [6, 3, 8, 9, 1, 7, 5, 2, 4] [2, 1] [1] [9, 4, 8, 3, 1, 6, 7, 2, 5, undef] [undef, 2, 1] [undef, 1] 0 3 3 3 3 0 1 1 2 1
The undefs running through the outputs of the for-loop implementation suggest a serious problem. The problem may stem from the notion that  scalar @array yields the index of the highest element in the array; in fact, it yields the number of elements in the array, and  $#array yields the highest index. (This assumes the default value of 0 for the Perl special, now deprecated, variable  $[ (see perlvar). I haven't checked, but maybe you can get correct behavior from the for-loop version if you change  $[ to 1 — but don't do that!)

Update: I've since checked, and it does seem as if the given for-loop implementation works correctly with  $[ == 1 — but again, don't do that!


Give a man a fish:  <%-{-{-{-<


In reply to Re: How do I shuffle an array? by AnomalousMonk
in thread How do I shuffle an array? by BramVanOosterhout

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.