in reply to Re^2: Randomly select values from array
in thread Randomly select values from array

A touch slow, but it works now:
use Algorithm::Permute qw(permute); @a = 'a' .. 'd'; $afact = 1; $afact *= $_ for 2 .. @a; permute { $h{ join '', sort { rand() < rand() } @a } += 1 / $afact; } @a for 1 .. 1e5; # a million takes a couple minutes printf "$_ : %.2f%%\n", $h{ $_ } / 1e3 for sort keys %h;

Replies are listed 'Best First'.
Re^4: Randomly select values from array
by BrowserUk (Patriarch) on May 16, 2010 at 19:11 UTC

    But why combine an O( N! ) algorithm with an O(N log N) algorithm, when Fischer-Yates is O(N)?

    List::Util::shuffle() does a million 4 way shuffles in less than 1 second.

      I was shooting for humorous understatement, guess it wasn't obvious enough. I agree it's a horrible idea to actually use, I was just practicing to see why the original sort version doesn't work for myself.