Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Randomly select values from array

by blakew (Monk)
on May 16, 2010 at 18:51 UTC ( [id://840249]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://840249]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2025-07-14 15:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.