To emphasis how bad that sort-based "shuffle" is, consider that over a million shuffles of four values, 1/3rd of the possibilities are never chosen. And the rest are chosen so disproportionately that 'fairness' doesn't enter the equation:
++$h{ join'',sort{ rand() < rand() } 'a'..'d' } for 1 .. 1e6;; printf "$_ : %.2f%%\n", $h{$_} / 1e4 for sort keys %h;; abcd : 12.48% abdc : 12.47% bacd : 12.48% badc : 12.49% cabd : 3.15% cadb : 3.12% cbad : 3.14% cbda : 3.13% cdab : 6.26% cdba : 6.26% dabc : 3.13% dacb : 3.15% dbac : 3.13% dbca : 3.11% dcab : 6.26% dcba : 6.25%
Contrast that with
use List::Util qw[ shuffle ];; ++$q{ join'',shuffle 'a'..'d' } for 1 .. 1e6;; printf "$_ : %.2f%%\n", $q{$_} / 1e4 for sort keys %q;; abcd : 4.18% abdc : 4.12% acbd : 4.14% acdb : 4.20% adbc : 4.17% adcb : 4.18% bacd : 4.15% badc : 4.19% bcad : 4.17% bcda : 4.16% bdac : 4.17% bdca : 4.15% cabd : 4.14% cadb : 4.17% cbad : 4.19% cbda : 4.18% cdab : 4.19% cdba : 4.20% dabc : 4.17% dacb : 4.15% dbac : 4.16% dbca : 4.22% dcab : 4.14% dcba : 4.13%
And the latter is more efficient to boot.
In reply to Re^2: Randomly select values from array
by BrowserUk
in thread Randomly select values from array
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |