Simple Probability Game
Heads or Tails? This script uses JavaScript's built-in Math.random() function to simulate random chance events like flipping a coin.
?
Copy the Script
<script>
function flipCoin() {
var outcome = Math.random(); // Returns 0.0 to 1.0
var resultText = "";
if (outcome < 0.5) {
resultText = "HEADS";
} else {
resultText = "TAILS";
}
document.getElementById("result").innerText = resultText;
}
</script>
<div id="result">Flip to see</div>
<button onclick="flipCoin()">Flip</button>
Frequently Asked Questions
It is 'pseudo-random', which is sufficient for games and UI effects. It is not secure enough for cryptography or gambling applications.
Yes. You simply need to call the random function multiple times and sum the results.
Yes. You can create custom logic (e.g., `if (Math.random() < 0.7)`) to make one outcome more likely than another.