Perl: the Markov chain saw PerlMonks

### Comment on

 Need Help??

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. :)

```#!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 displa
+ys

\$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";

In reply to RE: Spooky math problem (featuring perl!) by turnstep
in thread Spooky math problem by tilly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2017-08-23 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (350 votes). Check out past polls.

Notices?