Following on from this thread I thought, "enough with making LEDs blink, time for some probability!".
Taking the Monty Hall Problem from: http://marilynvossavant.com/game-show-problem/
Suppose you’re on a game show, and you’re given the choice of three doors. Behind one door is a car, behind the others, goats. You pick a door, say #1, and the host, who knows what’s behind the doors, opens another door, say #3, which has a goat. He says to you, "Do you want to pick door #2?" Is it to your advantage to switch your choice of doors?
Putting aside the mathematical treatment, let's try some code:
/*
Monty-Hall problem.
Author: Nick Gammon
Date: 25 August 2013
See: http://en.wikipedia.org/wiki/Monty_hall_problem
and: http://marilynvossavant.com/game-show-problem/
*/
const bool change = false; // whether or not to switch doors
const int seed = 1234;
const unsigned long rounds = 2000;
unsigned long wins;
void doOneRound ()
{
bool car [3];
for (byte i = 0; i < 3; i++)
car [i] = false;
// choose which door has the car
car [random (0, 3)] = true;
// contestant makes a random choice
byte choice = random (0, 3);
// host chooses another door
byte hosts_choice;
// don't choose contestant's door
// also don't reveal the car
do
{
hosts_choice = random (0, 3);
} while (hosts_choice == choice || car [hosts_choice]);
// change choice if desired
if (change)
{
if (choice != 0 && hosts_choice != 0)
choice = 0;
else if (choice != 1 && hosts_choice != 1)
choice = 1;
else
choice = 2;
} // end if
// did we win the car?
if (car [choice])
wins++;
} // end of doOneRound
void setup ()
{
Serial.begin (115200);
Serial.println ();
randomSeed (seed);
// do as many rounds as desired
for (unsigned long round = 0; round < rounds; round++)
doOneRound ();
Serial.print ("You won ");
Serial.print (100UL * wins / rounds);
Serial.println ("% of the time");
} // end of setup
void loop () { }
Before running it, do you predict:
You won 33% of the time
You won 50% of the time
You won 66% of the time
<something else>
(Or thereabouts, depending on the exact numbers from the random number generator).
You can change the line:
const bool change = false; // whether or not to switch doors
Make it "true" to change your decision about whether to switch doors, and see if it makes a difference.
Also see: Monty Hall problem - Wikipedia