Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^5: Randomly choosing from a set of alternatives with varying popularity

by ibm1620 (Hermit)
on Mar 28, 2022 at 13:45 UTC ( [id://11142462]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Randomly choosing from a set of alternatives with varying popularity
in thread Randomly choosing from a set of alternatives with varying popularity

I'm curious why the order in which the hash is traversed would matter at all. I tried both sorting and shuffling the hash keys on each roll of the dice and the overall distribution of choices remained (approximately) the same. Seems to me that as long as the percentage of votes remains the same, the randomness of the dice ensures that over time it'll land on each animal that percent of the time.

Example when 'popular' shuffles the candidate list each time. (Note that now the distribution of votes isn't linear.)

==> alligator:1000 bear: 900 cat: 800 dog: 700 elephant: 600 fox: 100 +giraffe: 10 hippo: 1 (Total votes: 4111) POP alligator: 977 bear: 933 cat: 804 dog: 720 elephant: 571 fox: 99 +giraffe: 6 hippo: 1 (Total votes: 4111)
I also tried sorting the candidates in both ascending and descending order. Same result.
  • Comment on Re^5: Randomly choosing from a set of alternatives with varying popularity
  • Download Code

Replies are listed 'Best First'.
Re^6: Randomly choosing from a set of alternatives with varying popularity
by LanX (Saint) on Mar 28, 2022 at 16:11 UTC
    > I'm curious why the order in which the hash is traversed would matter at all.

    It doesn't for this special algorithm, like already explained here

    But if you want the inverse - however "inversion" is defined - you need to build a new hash with accordingly "inverted" weights, like demonstrated here.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found