Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re: easy way to get random key from hash?

by jsprat (Curate)
on Aug 05, 2002 at 19:53 UTC ( [id://187797]=note: print w/replies, xml ) Need Help??


in reply to Re: easy way to get random key from hash?
in thread easy way to get random key from hash?

Hey! I was about to post the same algorithm (written slightly differently), found in perlfaq5: "How do I select a random line from a file?" You can find the proof by induction here, by guess who. Mine looked like this:

my ($i, $jokekey); ++$i && rand($i) < 1 && ($jokekey = $_) for (keys %hash);

Less readable than your sub, but same idea.

However, I wouldn't use it this way in production. Like you said, it always iterates through all the keys of the hash before it returns the random key. The benefit you gain by not needing to know how many elements isn't really a benefit. The following snippet will give you the same benefit and it's easier to read, and (probably) more efficient:

sub choose { #assumes 1st param is \%knockknocks my $jokeref = shift; my @setups = keys %$jokeref; return $setups[rand @setups]; }

Now if the jokes are kept in a separate file, that could change everything ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-25 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found