Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Someone asked for some perl?

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found