It might be more appropriate to do the shuffle before the uniq if the OP wants the duplicate values to have a greater chance of being selected - as suggested by the name @weighteddiv.
use List::Util 'shuffle';
use List::MoreUtils 'uniq';
use strict;
my @weighteddiv = (
(1..50),
(1..10) x 10,
);
my @shuffle1 = shuffle( uniq @weighteddiv );
my @shuffle2 = uniq( shuffle @weighteddiv );
printf "shuffle1: %s\n", join(', ', sort {$a <=> $b} @shuffle1[0..10])
+;
printf "shuffle2: %s\n", join(', ', sort {$a <=> $b} @shuffle2[0..10])
+;
Output:
shuffle1: 2, 5, 16, 17, 23, 25, 26, 27, 39, 42, 43
shuffle2: 1, 3, 4, 5, 6, 8, 9, 10, 13, 40, 50
This approach is appropriate for the current implementation of uniq, which preserves the original order while removing duplicates.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.