Monty Hall problem: Difference between revisions
Content added Content deleted
Underscore (talk | contribs) (Added Haskell; moved "Problem Statement" into lead) |
(Added Java) |
||
Line 116: | Line 116: | ||
100 * (fromIntegral n) / (fromIntegral trials)</pre> |
100 * (fromIntegral n) / (fromIntegral trials)</pre> |
||
=={{header|Java}}== |
|||
<java>import java.util.Random; |
|||
public class Monty{ |
|||
public static void main(String[] args){ |
|||
int[] doors = {0,0,0};//0 is a goat, 1 is a car |
|||
int switchWins = 0; |
|||
int stayWins = 0; |
|||
Random gen = new Random(); |
|||
for(int plays = 0;plays < 32768;plays++ ){ |
|||
doors[gen.nextInt(3)] = 1;//put a winner in a random door |
|||
int choice = gen.nextInt(3); //pick a door, any door |
|||
int shown; //the shown door |
|||
do{ |
|||
shown = gen.nextInt(3); |
|||
//don't show the winner or the choice |
|||
}while(doors[shown] != 1 && shown != choice); |
|||
stayWins += doors[choice];//if you wonby staying, count it |
|||
//could have switched to win |
|||
switchWins += (doors[choice] == 0)? 1: 0; |
|||
doors = new int[3];//clear the doors for the next test |
|||
} |
|||
System.out.println("Switching wins " + switchWins + " times."); |
|||
System.out.println("Staying wins " + stayWins + " times."); |
|||
} |
|||
}</java> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
<python> |
<python> |