Monty Hall problem: Difference between revisions

alphabetize
(Alphabetize)
(alphabetize)
Line 583:
Sample output:
staying: 33.73%, changing: 66.9%
 
=={{header|C sharp|C#}}==
{{trans|Java}}
 
<lang C sharp>
using System;
 
namespace MontyHallProblem
{
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];
}
 
System.Console.Out.Write("Switching wins " + switchWins + " times.");
System.Console.Out.Write("Staying wins " + stayWins + " times.");
}
}
}
</lang>
 
Sample output:
<pre>
Staying winns: 333830
Switching winns: 666170
</pre>
 
=={{header|D}}==
Line 1,904 ⟶ 1,955:
Staying winns: 3354
Switching winns: 6646
</pre>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
 
<lang C sharp>
using System;
 
namespace MontyHallProblem
{
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];
}
 
System.Console.Out.Write("Switching wins " + switchWins + " times.");
System.Console.Out.Write("Staying wins " + stayWins + " times.");
}
}
}
</lang>
 
Sample output:
<pre>
Staying winns: 333830
Switching winns: 666170
</pre>
Anonymous user