Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Monty-Hall-problem/C-sharp/monty-hall-problem.cs
Normal file
36
Task/Monty-Hall-problem/C-sharp/monty-hall-problem.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int switchWins = 0;
|
||||
int stayWins = 0;
|
||||
|
||||
Random gen = new Random();
|
||||
|
||||
for(int plays = 0; plays < 1000000; plays++ )
|
||||
{
|
||||
int[] doors = {0,0,0};//0 is a goat, 1 is a car
|
||||
|
||||
var winner = gen.Next(3);
|
||||
doors[winner] = 1; //put a winner in a random door
|
||||
|
||||
int choice = gen.Next(3); //pick a door, any door
|
||||
int shown; //the shown door
|
||||
do
|
||||
{
|
||||
shown = gen.Next(3);
|
||||
}
|
||||
while (doors[shown] == 1 || shown == choice); //don't show the winner or the choice
|
||||
|
||||
stayWins += doors[choice]; //if you won by staying, count it
|
||||
|
||||
//the switched (last remaining) door is (3 - choice - shown), because 0+1+2=3
|
||||
switchWins += doors[3 - choice - shown];
|
||||
}
|
||||
|
||||
Console.Out.WriteLine("Staying wins " + stayWins + " times.");
|
||||
Console.Out.WriteLine("Switching wins " + switchWins + " times.");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue