CyborgBeta
Banned
- Registriert
- Jan. 2021
- Beiträge
- 3.958
Moin,
ich bin mir nicht sicher, ob mein Simulationscode richtig ist, aber müsste statistisch gesehen die Wahrscheinlichkeit, die Ziege zu ziehen, nicht immer höher sein, wenn der Spieler nach der Frage des Quizmasters nicht die Tür wechselt?
Edit: Glaube, sehe den Fehler schon, der Quizmaster öffnet ja zunächst eine der beiden nicht gewählten Türen ... 😬
Edit 2: Das sollte jetzt auch richtig sein ... die Wahrscheinlichkeiten stimmen mit denen auf Wikipedia überein:
ich bin mir nicht sicher, ob mein Simulationscode richtig ist, aber müsste statistisch gesehen die Wahrscheinlichkeit, die Ziege zu ziehen, nicht immer höher sein, wenn der Spieler nach der Frage des Quizmasters nicht die Tür wechselt?
Java:
import java.util.Random;
public class Goat {
public static void main(String[] args) {
int switchDoor = 0;
int noSwitchDoor = 0;
for (int i = 0; i < 1_000_000; i++) {
switchDoor += simulate(true);
noSwitchDoor += simulate(false);
}
System.out.printf("Switch door: %f%%%n", switchDoor / 10_000.);
System.out.printf("No switch door: %f%%%n", noSwitchDoor / 10_000.);
}
private static int simulate(boolean switchDoor) {
Random random = new Random();
final int goatAt = random.nextInt(3);
final int playerChoose1 = random.nextInt(3);
if (goatAt == playerChoose1) {
// The player has chosen the door with the goat behind it.
return 0;
}
int playerChoose2;
do {
playerChoose2 = random.nextInt(3);
} while (playerChoose2 == playerChoose1);
// Ask the player to switch the chosen door:
if (switchDoor) {
playerChoose2 = playerChoose1 ^ playerChoose2 ^ 3;
}
if (playerChoose2 == goatAt) {
// The player has chosen the door with the goat behind it.
return 1;
}
// The player has chosen the door with the car behind it.
return 2;
}
}
Edit: Glaube, sehe den Fehler schon, der Quizmaster öffnet ja zunächst eine der beiden nicht gewählten Türen ... 😬
Edit 2: Das sollte jetzt auch richtig sein ... die Wahrscheinlichkeiten stimmen mit denen auf Wikipedia überein:
Java:
import java.util.Random;
public class Goat {
private static final Random random = new Random();
public static void main(String[] args) {
int switchDoor = 0;
int noSwitchDoor = 0;
for (int i = 0; i < 1_000; i++) {
switchDoor += simulate(true);
noSwitchDoor += simulate(false);
}
System.out.printf("Switch door: %f%%%n", switchDoor / 10.);
System.out.printf("No switch door: %f%%%n", noSwitchDoor / 10.);
}
private static int simulate(boolean switchDoor) {
final int winAt = random.nextInt(3);
final int playerChoose1 = random.nextInt(3);
int quizMasterChoose;
// The quiz master chooses a door that is not the one the player has chosen, and not the one with the car behind it:
do {
quizMasterChoose = random.nextInt(3);
} while (quizMasterChoose == playerChoose1 || quizMasterChoose == winAt);
// Ask the player to switch the chosen door:
final int playerChoose2 = switchDoor ? playerChoose1 ^ quizMasterChoose ^ 3 : playerChoose1;
if (playerChoose2 == winAt) {
// The player has chosen the door with the car behind it.
return 1;
}
// The player has chosen the door with a goat behind it.
return 0;
}
}
Zuletzt bearbeitet: