No such thing as a small change PerlMonks

### RE: Spooky math problem (featuring perl!)

by turnstep (Parson)
 on Nov 01, 2000 at 22:49 UTC Need Help??

in reply to Spooky math problem

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";
[download]```

Replies are listed 'Best First'.
OK, understood, but still 1 *huge* flaw
by Fastolfe (Vicar) on Nov 01, 2000 at 23:35 UTC
Update: OK I'm re-reading everything and I'm still not sure that I understand it after all.. *sigh*..

For those that don't get it, like I didn't, realize that we're making one important assumption here: that both parties know the upper and lower bounds of the numbers!

Sure, if we know we're working from 0 .. 100, and the envelope we see is number 88, odds are, it's the higher of the two envelopes (assuming both are truly random; this can be defeated if the "dealer" keeps writing two consecutive numbers or otherwise knows the trick). Now I look at the situation and think, "Well yah, duh."

But what if you don't know the upper bounds? Modify the lines in the script like below, and the odds come out to exactly 50%: what we'd expect. The way *I* would play this game in real life, without knowing the limit, is to try to "figure out" the limit as I go, but statistically, given a one-time shot, we have to assume that the number we were given is, on average, mid-way through the distribution. This means that no matter what number we think of, we're still 50% likely of guessing right.

```...
\$letter1 = \$money[rand @money / 2];
{ \$letter2  = \$money[rand @money / 2]; redo if \$letter1 == \$letter2;
+ }
...
{ \$guess = \$money[rand ((\$letter2+1) * 2)]; redo if \$guess == \$lette
+r2; }
...
[download]```
But if we know we're going to play 100 games, we can sorta "figure out" the upper limit as we go (assuming there is one, and it's kept constant during play), by assuming that our numbers will average out to be mid-way through the distribution. That would let us approach a higher probability of being correct further down the line. The script can be modified to play by those rules too, but I'll leave that as an exercise for the reader.

If you were to actually make these bold statements and try them in real life (give them 10 cards, pick two at random, show me one and I'll guess 66% correctly whether it's the higher of the two cards), I would seriously hope that they would figure out the trick without me needing to guess very much, if at all. If they were smart, they'd shut up when they figured it out and would keep pulling, say 9 and 10, and showing me the 9 all the time.

No such assumption is made in the problem.

With a continuous probability distribution you can have some probability of picking a number in any range at all. The probability may be low, but it is always positive. That is why I said "non-zero density everywhere" in the details.

More carefully stated the experiment is defined as, "I hand you one of my two numbers at random, you look at it, hand it back, and make a guess as to high or low." The assertion is that there is a method you can use such that, no matter what my numbers are, you will have better than even odds of being right. What your odds are will depend, of course, on what my numbers are. All that you can guarantee is better than even.

Of course any purely mechanical computer will have the usual problems with "randomly pick out of a continuous distribution" however you can specify a concrete algorithm for doing that with coin-flipping (note that you don't need to find the number precisely, just determine it to sufficient detail to compare it with the one you were handed). You need make no assumptions on my numbers. And an outside observer who knows both my numbers and your method can calculate the exact probability you will be right - and that will be better than even.

Guaranteed.

(Yes, this skirts on the boundary of a lot of interesting problems, philosophical and otherwise, in math.)

Whoa. What tilly is claiming is that we have a "better than 50% chance" of getting it right. The degree to which it's "better" can be *mind-bogglingly miniscule* (we're talking continuous quantities here) so I don't think we're warranted in exporting any claims about the actual probabilities from what a program reports.

Philosophy can be made out of anything. Or less -- Jerry A. Fodor

I don't care what the theory is. I may not understand it, in which case my changes to the code are non-sensical, but they basically boil down to this:
```\$num1  = rand(1000);
\$num2  = rand(1000);  # pretend \$num2 != \$num1

\$guess = rand(\$num1 * 2);
[download]```
Are we asserting that \$num2 > \$guess more than 50% of the time?

Log In?
 Username: Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://39447]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-08-15 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When will the AI bubble burst?

Results (32 votes). Check out past polls.

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.