note
turnstep
<P>Someone asked for some perl?</P>
<P>Try this out with a small range (3) and then
boost it up to 100 and watch the difference. Running
at least 100,000 games is recommended but play around
with the variables. :)</P>
<CODE>
#!perl
## HiLowDeal
use strict;
## This program has a "dealer" and a "player"
## The dealer generates two random numbers and sticks them
## into two envelopes. One of these is handed to the player.
## The player looks at her number, then picks another
## number at random and assumes that she has picked the
## dealer's number. She then either states that she has
## the high or the low envelope. 50/50? Perhaps not...
## Usage: HiLowDeal 10 10000 50
## The above example plays ten thousand games,
## uses the numbers 1 through 10 to draw the random numbers from,
## and outputs a running total about 50 times
my $games = shift || 10000; ## How many games shall we play?
my $limit = shift || 10; ## What range of numbers?
my $checkpoint = shift || 3; ## Approximate # of checkpoint displays
$checkpoint = int $games/$checkpoint;
## The total set is really an infinite set, but we
## will restrain our players to this without letting
## them "know" the range, making it effectively infinite.
## In other words, if a "10" is received, they do not know
## that there is no "11", "12", etc... and cannot automatically
## deduce that they have the higher card.
my @money=(1..$limit);
print "Games=$games, from 1 to $limit, checkpoint is $checkpoint.\n";
my ($letter1, $letter2);
my ($guess, $result, $actual);
my $total=0; my $counter=0;
for (1..$games) {
## Select two random cards, must be different
$letter1 = $money[rand @money];
{ $letter2 = $money[rand @money]; redo if $letter1 == $letter2; }
## Player one has both, letters, then one to player #2.
## Player two looks at his, then makes a guess as to the other one.
## Cannot guess their own number
{ $guess = $money[rand @money]; redo if $guess == $letter2; }
## The player's best guess: (1 means they have the higher card)
$result = $letter2 > $guess ? 1 : 0;
## The actual value:
$actual = $letter2 > $letter1 ? 1 : 0;
## Are they correct? If so, give them a point. If not, take one away:
$total += ($result==$actual) ? +1 : -1;
if (++$counter == $checkpoint) {
$counter=0;
print "TOTAL: $total\n";
}
}
print "Grand total: $total\n";
</CODE>
<!-- It's a quick job, so suggestions welcome :) -->
39366
39366