Guest User

Untitled

a guest
Jun 26th, 2025
27
0
94 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.22 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Monty Hall Simulator</title>
  7.     <style>
  8.         body {
  9.             font-family: Arial, sans-serif;
  10.             max-width: 600px;
  11.             margin: 0 auto;
  12.             padding: 20px;
  13.             text-align: center;
  14.         }
  15.         button {
  16.             padding: 10px 20px;
  17.             font-size: 16px;
  18.             background: #4CAF50;
  19.             color: white;
  20.             border: none;
  21.             border-radius: 5px;
  22.             cursor: pointer;
  23.         }
  24.         button:hover {
  25.             background: #45a049;
  26.         }
  27.         #results {
  28.             margin-top: 20px;
  29.             text-align: left;
  30.             padding: 10px;
  31.             border: 1px solid #ddd;
  32.             border-radius: 5px;
  33.             background: #f9f9f9;
  34.         }
  35.         .win {
  36.             color: green;
  37.             font-weight: bold;
  38.         }
  39.         .loss {
  40.             color: red;
  41.         }
  42.     </style>
  43. </head>
  44. <body>
  45.     <h1>Monty Hall Problem Simulator</h1>
  46.     <p>Click the button to run 100 trials. See how often you win by <b>staying</b> vs. <b>switching</b>.</p>
  47.    
  48.     <button onclick="runSimulation()">Run 100 Simulations</button>
  49.    
  50.     <div id="results"></div>
  51.  
  52.     <script>
  53.         function runSimulation() {
  54.             let stayWins = 0;
  55.             let switchWins = 0;
  56.             const resultsDiv = document.getElementById('results');
  57.             resultsDiv.innerHTML = '<h3>Running 100 trials...</h3>';
  58.  
  59.             // Delay to allow UI to update before heavy computation
  60.             setTimeout(() => {
  61.                 for (let i = 0; i < 100; i++) {
  62.                    // 1. Place the car randomly behind doors 0, 1, or 2
  63.                    const carDoor = Math.floor(Math.random() * 3);
  64.                    
  65.                    // 2. Contestant picks a random door
  66.                    const initialPick = Math.floor(Math.random() * 3);
  67.                    
  68.                    // 3. Host opens a goat door (not the contestant's pick, not the car)
  69.                    let hostOpens;
  70.                    do {
  71.                        hostOpens = Math.floor(Math.random() * 3);
  72.                    } while (hostOpens === initialPick || hostOpens === carDoor);
  73.                    
  74.                    // 4. Determine outcomes
  75.                    if (initialPick === carDoor) {
  76.                        stayWins++; // Staying wins
  77.                    } else {
  78.                        switchWins++; // Switching wins (since car is the remaining door)
  79.                    }
  80.                }
  81.                // Display results
  82.                resultsDiv.innerHTML = `
  83.                    <h3>Results after 100 trials:</h3>
  84.                     <p><b>Staying with initial choice:</b> <span class="win">${stayWins} wins</span> (~${Math.round(stayWins)}%)</p>
  85.                     <p><b>Switching doors:</b> <span class="win">${switchWins} wins</span> (~${Math.round(switchWins)}%)</p>
  86.                     <p><i>Theoretical expectation: ~33% stay wins, ~66% switch wins.</i></p>
  87.                 `;
  88.             }, 10);
  89.         }
  90.     </script>
  91. </body>
  92. </html>
Advertisement
Add Comment
Please, Sign In to add comment