Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Ethan on 10/26/16.
- */
- //TEST OUTPUT:
- //Switch Games ->
- //Total games: 1041359347
- //Total wins: 694272112
- //Ratio: 0.6666978528241282
- //
- //Stay Games ->
- //Total games: 1066094998
- //Total wins: 355383475
- //Ratio: 0.3333506629333671
- //
- //Random Games ->
- //Total games: 676058595
- //Total wins: 338024412
- //Ratio: 0.499992773555375
- public class MonteyHall {
- public static void main(String[] args) throws InterruptedException {
- boolean shouldStop = false;
- MonteyHallGame[] games = new MonteyHallGame[3];
- games[0] = new MonteyHallGame();
- games[1] = new MonteyHallGame();
- games[2] = new MonteyHallGame();
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- games[0].playSwitchGame();
- }
- }
- }).start();
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- games[1].playStayGame();
- }
- }
- }).start();
- new Thread(new Runnable() {
- @Override
- public void run() {
- while(true) {
- if (Math.random() > .5) {
- games[2].playStayGame();
- } else {
- games[2].playSwitchGame();
- }
- }
- }
- }).start();
- Thread.sleep(900000);
- System.out.printf("Switch Games ->%nTotal games: %s%nTotal wins: %s%nRatio: %s%n%n",
- games[0].getNumOfGamesPlayed(), games[0].getNumOfGamesWon(), (double) games[0].getNumOfGamesWon() / games[0].getNumOfGamesPlayed());
- System.out.printf("Stay Games ->%nTotal games: %s%nTotal wins: %s%nRatio: %s%n%n",
- games[1].getNumOfGamesPlayed(), games[1].getNumOfGamesWon(), (double) games[1].getNumOfGamesWon() / games[1].getNumOfGamesPlayed());
- System.out.printf("Random Games ->%nTotal games: %s%nTotal wins: %s%nRatio: %s%n%n",
- games[2].getNumOfGamesPlayed(), games[2].getNumOfGamesWon(), (double) games[2].getNumOfGamesWon() / games[2].getNumOfGamesPlayed());
- System.exit(1); // because i was too lazy to properly multithread, this essentially crashes it. (immediate stop)
- }
- }
- /**
- * Created by Ethan on 10/26/16.
- */
- class MonteyHallGame {
- private long numOfGamesPlayed = 0;
- private long numOfGamesWon = 0;
- public void playStayGame() {
- boolean[] doors = new boolean[3];
- int selectedDoor = (int) (Math.random() * 3);
- doors[(int) (Math.random() * 3)] = true;
- if (doors[selectedDoor]) {
- ++numOfGamesWon;
- }
- ++numOfGamesPlayed;
- }
- public void playSwitchGame() {
- boolean[] doors = new boolean[3];
- int selectedDoor = (int) (Math.random() * 3);
- doors[(int) Math.random() * 3] = true;
- // remove wrong door
- for (int i = 0; i < doors.length; i++) {
- if (!doors[i] && selectedDoor != i) {
- if (!doors[selectedDoor]) {
- for (int j = 0; j < doors.length; j++) {
- if (j == i) {
- continue;
- } else if (j != selectedDoor) {
- if (doors[j]) {
- ++numOfGamesWon;
- }
- }
- }
- }
- }
- }
- ++numOfGamesPlayed;
- }
- public long getNumOfGamesPlayed() {
- return numOfGamesPlayed;
- }
- public long getNumOfGamesWon() {
- return numOfGamesWon;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement