Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Monty Hall Simulator</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- max-width: 600px;
- margin: 0 auto;
- padding: 20px;
- text-align: center;
- }
- button {
- padding: 10px 20px;
- font-size: 16px;
- background: #4CAF50;
- color: white;
- border: none;
- border-radius: 5px;
- cursor: pointer;
- }
- button:hover {
- background: #45a049;
- }
- #results {
- margin-top: 20px;
- text-align: left;
- padding: 10px;
- border: 1px solid #ddd;
- border-radius: 5px;
- background: #f9f9f9;
- }
- .win {
- color: green;
- font-weight: bold;
- }
- .loss {
- color: red;
- }
- </style>
- </head>
- <body>
- <h1>Monty Hall Problem Simulator</h1>
- <p>Click the button to run 100 trials. See how often you win by <b>staying</b> vs. <b>switching</b>.</p>
- <button onclick="runSimulation()">Run 100 Simulations</button>
- <div id="results"></div>
- <script>
- function runSimulation() {
- let stayWins = 0;
- let switchWins = 0;
- const resultsDiv = document.getElementById('results');
- resultsDiv.innerHTML = '<h3>Running 100 trials...</h3>';
- // Delay to allow UI to update before heavy computation
- setTimeout(() => {
- for (let i = 0; i < 100; i++) {
- // 1. Place the car randomly behind doors 0, 1, or 2
- const carDoor = Math.floor(Math.random() * 3);
- // 2. Contestant picks a random door
- const initialPick = Math.floor(Math.random() * 3);
- // 3. Host opens a goat door (not the contestant's pick, not the car)
- let hostOpens;
- do {
- hostOpens = Math.floor(Math.random() * 3);
- } while (hostOpens === initialPick || hostOpens === carDoor);
- // 4. Determine outcomes
- if (initialPick === carDoor) {
- stayWins++; // Staying wins
- } else {
- switchWins++; // Switching wins (since car is the remaining door)
- }
- }
- // Display results
- resultsDiv.innerHTML = `
- <h3>Results after 100 trials:</h3>
- <p><b>Staying with initial choice:</b> <span class="win">${stayWins} wins</span> (~${Math.round(stayWins)}%)</p>
- <p><b>Switching doors:</b> <span class="win">${switchWins} wins</span> (~${Math.round(switchWins)}%)</p>
- <p><i>Theoretical expectation: ~33% stay wins, ~66% switch wins.</i></p>
- `;
- }, 10);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment