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); |
|||
int switchWins = 0, stayWins = 0; |
|||
while(switchWins + stayWins < 100_000) { |
|||
int carPos = dice(gen, 1, 1, 1); // Which door is car behind? |
|||
int pickPos = dice(gen, 1, 1, 1); // Contestant's initial pick. |
|||
int openPos; // Which door is opened by Monty Hall? |
|||
// Monty can't open the door you picked or the one with the car |
|||
// behind it. |
|||
do { |
|||
openPos = dice(gen, 1, 1, 1); |
|||
} while(openPos == pickPos || openPos == carPos); |
|||
int switchPos = 0; |
|||
// Find position that's not currently picked by contestant and |
|||
// was not opened by Monty already. |
|||
for (; pickPos==switchPos || openPos==switchPos; switchPos++) {} |
|||
if (pickPos == carPos) |
|||
stayWins++; |
|||
else if (switchPos == carPos) |
|||
switchWins++; |
|||
else |
|||
assert(0); // Can't happen. |
|||
} |
|||
} |
|||
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#}}== |