Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by LanX (Saint)
on Mar 28, 2022 at 11:32 UTC ( [id://11142461]=note: print w/replies, xml ) Need Help??


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

> weight by the reciprocal of the votes

FWIW, here my attempts with different reciprocal functions and evaluation of distribution. See also Re^3: Randomly choosing from a set of alternatives with varying popularity for interpretation.

use strict; use warnings; use Data::Dump qw/pp dd/; my %vote = ( alligator => 100, bear => 90, cat => 80, jellyfish => 10 +); my $N =0; $N += $_ for values %vote; # --- invert by division, proportion of non-voters my $N2 = 0; my %vote2 = map { my $v2 = 1/$vote{$_}; $N2 += $v2; $_ => $v2 } keys %vote; # --- invert by subtraction, number of non-voters my $N3 = 0; my %vote3 = map { my $v3 = $N-$vote{$_}; $N3 += $v3; $_ => $v3 } keys %vote; my (%dist1,%dist2,%dist3); my $e=5; $dist1{pick($N,\%vote)}++ for 1..10**$e; $dist2{pick($N2,\%vote2)}++ for 1..10**$e; $dist3{pick($N3,\%vote3)}++ for 1..10**$e; pp \%dist1,\%dist2,\%dist3; sub pick { my ($N,$h_vote)=@_; my @deb; my $r =rand($N); push @deb,$r; scalar keys %$h_vote; # reset each while ( my ($k,$v) = each %$h_vote) { $r -= $v; push @deb,[$r,$k,$v]; return $k if $r <=0; } die "ERROR", pp \@deb; }
OUTPUT:
( { alligator => 35891, bear => 31807, cat => 28792, jellyfish => 3510 + }, { alligator => 7497, bear => 8300, cat => 9481, jellyfish => 74722 } +, { alligator => 21416, bear => 22542, cat => 23870, jellyfish => 3217 +2 }, )

On a tangent: had to debug why each failed me. Needed reset.

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

Replies are listed 'Best First'.
Re^3: Randomly choosing from a set of alternatives with varying popularity
by ibm1620 (Hermit) on Mar 29, 2022 at 00:39 UTC
    The reciprocal approach definitely gives me what I had in mind, more so than computing the non-voters. That's what I'll use. Thanks.

    (Incidentally, should your comment above read "invert by division, proportion of voters", or am I missing something?)

      > That's what I'll use. Thanks.

      Your welcome, but you should also thank hv. :)

      > (Incidentally, should your comment above read "invert by division, proportion of voters", or am I missing something?)

      It's a matter of perspective, (proportion of non-voters) = 1-(proportion of voters)

      If there is a better wording, I'd be happy to hear it.

      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://11142461]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found