How to write a simulation program
I would like to learn how to write games in Java,for instance the Monte
Hall Paradox..asks the user if she wants to "always switch" or "never switch"
then simulate the games 10,000 times given the decision.
Then print out the fraction of times the player wins.
something like this:
public class Game {
public static void main(String args[]){
Random r = new Random();
int prizeDoor = r.nextInt(3);
int choice = r.nextInt(3);
System.out.println("Would you like to ALWAYS switch:? ");
.....
System.out.println("Would you like to NEVER switch? ");
.....
//Print out fraction of time user wins
int times;
if(){}
System.out.println("User win: " + times ");
}
}
Not quite sure if im on the right track or how to go about it..any help is greatly appreciated.
[878 byte] By [
exl5a] at [2007-11-27 6:32:54]

You're basic setup so far looks at least ok.. But one part concerns me a little bit..
System.out.println("Would you like to ALWAYS switch:? ");
System.out.println("Would you like to NEVER switch? ");
Does the player have the option of picking no to both? From what you said, it sounds like this is a one-or-the-other choice. There should be no reason to ask the question twice. You should be able to just ask them which they prefer and store that result in a boolean value.
Then you simply have to simulate the Monte Hall game within your if block and keep track of wins/losses.
> From what you said, it sounds like this is a one-or-the-other choice. There should be no reason to ask the question twice.
Close.
Furthermore, one is just 1 - prob of the other so you shouldnt ask either question.
Ive written the Monty Hall problem before let me see if I can find it.
I couldnt find my program so I rewrote it:
import java.util.Random;
public class MontyHallGame{
public static void main(String[] args){
new MontyHallGame();
}
public MontyHallGame(){
int iterations = 1000;
int switchWins = 0;
for(int i = 0; i < iterations; i++){
if(playSingleGame()){ switchWins ++; }
}
System.out.println("\n=== MONTY HALL GAME ===");
System.out.println("\nWins: " + switchWins);
double winPercentage = ((double)switchWins / (double)iterations) * 100;
System.out.println("\nWin Percentage: " + winPercentage);
System.out.println("Lose Percentage: " + (100 - winPercentage));
}
public boolean playSingleGame(){
boolean won = false;
int prizeDoor = random.nextInt(3) + 1;
int initGuess = random.nextInt(3) + 1;
if(initGuess != prizeDoor){
won = true;
}
return won;
}
Random random = new Random();
}
> I don't get it? Whats the point of the Game?
On the game show Let's Make a Deal, contestants would get to pick one door out of three. One of the three had a nice prize, and the other two had small prizes or nothing.
After picking, the host (Monty Hall) would open one door and show one of the shitty or nonexistent prizes. Then he'd ask the contestant if he wanted to stick with his original choice, or switch to the other door. The better choice is to switch, because you have a 2/3 chance of winning if you switch, and only a 1/3 if you don't.
The point is to try to guess which door has the big prize behind it.
The purpose of this program is to simulate that game, and show that if you switch, you'll win 2/3 of the time, and if you don't, you'll lose 2/3 of the time.
jverda at 2007-7-12 17:58:37 >
