[lapm] RE: [LA.pm] The odds of finger games
Robert Spier
rspier at pobox.com
Wed Mar 5 19:15:01 CST 2003
> oblapm: Robert, did you also calculate the probability
> distribution of your single finger, binary, modulus, algorithm?
> If I understand it correctly, I think it will give some lower
> numbered participants an unfair advantage unless the number of
> participants is a power of 2.
0: 11
1: 11
2: 11
3: 11
4: 10
5: 10
You are correct, sir.
Also, there are some subtle issues in my first program. It works
correctly because the numbers happened to work out. (6 states, 6
people.) It wasn't as general as it should have been, probably
because I wrote it in a post-lunch stupor.
This version only supports binary digits.
use strict;
use warnings;
use Set::CrossProduct;
my $states = 2; # 0 fingers, 1 finger
my $hand = [0..$states -1];
my $people = 6;
my $it = Set::CrossProduct->new( [($hand)x $people] );
my %sums = ();
my %modsums = ();
while (my $tuple = $it->get) {
my $n = join "",@$tuple;
my $t = unpack("N",pack("B32",substr("0" x 32 . $n,-32)));
$sums{$t}++;
$modsums{$t%$people}++;
}
# Output non-mod-ed values (sum of all fingers)
#print "$_: $sums{$_}\n"
# for (sort {$a <=> $b} keys %sums);
# Output mod-ed values, i.e. "people"
print "$_: $modsums{$_}\n"
for (sort {$a <=> $b} keys %modsums);
And this version is the general purpose brute force solution:
use strict;
use warnings;
use Set::CrossProduct;
use Math::BaseCalc;
#my $states = 2; # 0 fingers, 1 finger
my $states = 10;
my $hand = [0..$states -1];
my $people = 6;
my $it = Set::CrossProduct->new( [($hand)x $people] );
my $ba = new Math::BaseCalc(digits => $hand);
my %sums = ();
my %modsums = ();
while (my $tuple = $it->get) {
my $t = $ba->from_base( join "",@$tuple );
$sums{$t}++;
$modsums{$t%$people}++;
}
# Output non-mod-ed values (sum of all fingers)
#print "$_: $sums{$_}\n"
# for (sort {$a <=> $b} keys %sums);
# Output mod-ed values, i.e. "people"
print "$_: $modsums{$_}\n"
for (sort {$a <=> $b} keys %modsums);
More information about the Losangeles-pm
mailing list