Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Pulling random elements from hash

by George_Sherston (Vicar)
on Jun 22, 2003 at 21:17 UTC ( #267989=note: print w/ replies, xml ) Need Help??


in reply to Pulling random elements from hash

Yet another way to do it:
print $hash{$_} for (sort {rand() <=> 0.5} keys %hash)

George Sherston


Comment on Re: Pulling random elements from hash
Download Code
Re: Re: Pulling random elements from hash
by ihb (Deacon) on Jun 22, 2003 at 23:21 UTC
    Perlfunc / sort: The comparison function is required to behave. If it returns inconsistent results (sometimes saying $x[1] is less than $x[2] and sometimes saying the opposite, for example) the results are not well-defined.

    I guess that means you don't really know what your code will give you. (Which kind of seems to be the point of it, but in another way. :))

    ihb
Re^2: Pulling random elements from hash
by Aristotle (Chancellor) on Jun 30, 2003 at 20:42 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-10-22 01:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls