another thing (not optimized but i hope clear enough) not exactly for the same use (i needed to keep the values of a column) so it is more an array to anonymise some labels. i use it for blind tests
my @tab_anonyme; for $i (0..$#my_data) { $tab_anonyme[$i][0] = $my_data[$i][0]; # data to keep $tab_anonyme[$i][1] = int ( rand (100) ) + 1; # anonyme label } # verification my $flag_duplicate; do { $flag_duplicate = "false"; for $i (0..$#tab_anonyme) { for $i2 (0..$#tab_anonyme) { if ($i2 == $i) { next; } # no testing of a value against i +tself if ( $tab_anonyme[$i][1] == $tab_anonyme[$i2][1] ) { $tab_anonyme[$i2][1] = int ( rand (100) ) + 1; $flag_duplicate = "true"; } } } } while ( $flag_doublon eq "true" );
two problems

- in rand(X), if X is smaller than the number of lines of the array @mydata => infinite loop, make it four or five time bigger

- time taken is not very predictible


In reply to Re: random index in array - no duplicates by Anonymous Monk
in thread random index in array - no duplicates by emilford

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.