Monty Hall problem: Difference between revisions

Content added Content deleted
m (→‎Icon and Unicon: header simplification)
Line 639: Line 639:


void main() {
void main() {
Random gen = Random(unpredictableSeed);
Random gen = Random(unpredictableSeed);
uint switchWins = 0, stayWins = 0;
int switchWins = 0, stayWins = 0;


while(switchWins + stayWins < 100_000) {
while(switchWins + stayWins < 100_000) {
uint carPos = dice(gen, 1, 1, 1); // Which door is car behind?
int carPos = dice(gen, 1, 1, 1); // Which door is car behind?
uint pickPos = dice(gen, 1, 1, 1); // Contestant's initial pick.
int pickPos = dice(gen, 1, 1, 1); // Contestant's initial pick.
uint openPos; // Which door is opened by Monty Hall?
int openPos; // Which door is opened by Monty Hall?


// Monty can't open the door you picked or the one with the car
// Monty can't open the door you picked or the one with the car
// behind it.
// behind it.
do {
do {
openPos = dice(gen, 1, 1, 1);
openPos = dice(gen, 1, 1, 1);
} while(openPos == pickPos || openPos == carPos);
} while(openPos == pickPos || openPos == carPos);


uint switchPos = 0;
int switchPos = 0;
// Find position that's not currently picked by contestant and was
// Find position that's not currently picked by contestant and
// not opened by Monty already.
// was not opened by Monty already.
for(; pickPos == switchPos || openPos == switchPos; switchPos++) {}
for (; pickPos==switchPos || openPos==switchPos; switchPos++) {}


if(pickPos == carPos) {
if (pickPos == carPos)
stayWins++;
stayWins++;
} else if(switchPos == carPos) {
else if (switchPos == carPos)
switchWins++;
switchWins++;
} else {
else
assert(0); // Can't happen.
assert(0); // Can't happen.
}
}
}


writefln("Switching wins: %d Staying wins: %d", switchWins, stayWins);
writefln("Switching/Staying wins: %d %d", switchWins, stayWins);
}</lang>
}</lang>

Output:
Output:
<pre>Switching/Staying wins: 66609 33391</pre>
<pre>
Switching wins: 66673 Staying wins: 33327
</pre>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==