Coin Flip
Flip a fair coin: heads or tails.
Back to all tools on ToolForge
About Coin Flip
This coin flip tool gives you a fair heads-or-tails result.
It is useful for quick choices, games, experiments and simple tie-breakers when you want a neutral random outcome.
Understanding Random Number Generation
Computer-based coin flips use pseudo-random number generators (PRNGs):
- Algorithm: Mathematical formula produces seemingly random sequences
- Seed value: Initial input determines the entire sequence (often system time)
- Periodicity: PRNG sequences eventually repeat (period length varies by algorithm)
- Distribution: Good PRNGs produce uniform distribution across the output range
- Determinism: Same seed always produces identical sequence (reproducible for testing)
JavaScript Random Implementation
// Basic coin flip using Math.random()
function flip() {
const result = Math.random() < 0.5 ? "Heads" : "Tails";
return result;
}
// Cryptographically secure alternative
function secureFlip() {
const array = new Uint32Array(1);
crypto.getRandomValues(array);
return array[0] % 2 === 0 ? "Heads" : "Tails";
}
// Multiple flips simulation
function flipMultiple(count) {
let heads = 0, tails = 0;
for (let i = 0; i < count; i++) {
if (Math.random() < 0.5) heads++;
else tails++;
}
return { heads, tails, ratio: heads / count };
}
Probability and Statistics
| Concept | Formula | Example |
|---|---|---|
| Single flip probability | P(heads) = 1/2 = 0.5 | 50% chance |
| Two consecutive heads | P(HH) = 1/2 × 1/2 = 1/4 | 25% chance |
| Three consecutive heads | P(HHH) = 1/2³ = 1/8 | 12.5% chance |
| At least one heads in n flips | P = 1 - (1/2)ⁿ | 99.9% in 10 flips |
| Expected heads in n flips | E = n × 0.5 | 50 heads in 100 flips |
| Standard deviation | σ = √(n × p × (1-p)) | 5 for 100 flips |
Random Number Generator Comparison
| Method | Type | Speed | Security | Use Case |
|---|---|---|---|---|
| Math.random() | PRNG | Very Fast | Not secure | Games, simulations, casual decisions |
| crypto.getRandomValues() | CSPRNG | Fast | Secure | Passwords, tokens, cryptography |
| Physical coin | TRNG | Manual | Unpredictable | In-person decisions, demonstrations |
| Atmospheric noise | TRNG | Slow | Secure | Lotteries, gambling, research |
Common Coin Flip Applications
- Sports: Deciding which team gets first possession (football, cricket)
- Gaming: Determining turn order, random events, loot drops
- Decision Making: Choosing between equally preferred options
- Research: Random assignment in experiments and A/B tests
- Education: Teaching probability, statistics, and randomness concepts
- Dispute Resolution: Fair, unbiased tiebreaker for disagreements
Testing Randomness Quality
// Chi-squared test for fairness
function testFairness(flips = 10000) {
let heads = 0;
for (let i = 0; i < flips; i++) {
if (Math.random() < 0.5) heads++;
}
const expected = flips / 2;
const chiSquared = Math.pow(heads - expected, 2) / expected +
Math.pow(flips - heads - expected, 2) / expected;
const pValue = 1 - chiSquaredCDF(chiSquared, 1); // df = 1
return { heads, expected, chiSquared, pValue };
}
// p-value > 0.05 suggests fair distribution
Frequently Asked Questions
- How does computer-generated random number generation work?
- JavaScript's Math.random() uses a pseudo-random number generator (PRNG) algorithm, typically a linear congruential generator or xorshift algorithm. It produces deterministic sequences from an initial seed (often system time). While statistically distributed, PRNGs are not truly random and not suitable for cryptographic purposes. For security, use crypto.getRandomValues().
- What is the difference between pseudo-random and true random?
- Pseudo-random (PRNG) uses mathematical algorithms with deterministic output from a seed. True random (TRNG) derives randomness from physical phenomena like atmospheric noise, thermal noise, or quantum effects. PRNGs are fast and reproducible; TRNGs are slower but unpredictable. Coin flips use PRNGs which are sufficient for casual decisions.
- Is Math.random() truly 50/50 for coin flips?
- Math.random() produces uniformly distributed values between 0 and 1 with equal probability across the range. Comparing against 0.5 gives theoretically equal heads/tails probability. However, PRNG periodicity and seed quality can introduce microscopic biases. For casual use, the distribution is effectively fair; for statistical applications, use crypto APIs.
- What is the Web Crypto API and when should it be used?
- The Web Crypto API (crypto.getRandomValues()) provides cryptographically secure random numbers from the operating system's entropy pool. It uses hardware events, mouse movements, and other unpredictable sources. Use for passwords, tokens, gambling, lotteries, or any security-sensitive application. Not needed for casual coin flips.
- What are common use cases for coin flip simulations?
- Coin flips resolve binary decisions fairly: choosing teams in games, settling disputes, A/B test assignment, randomized experiments, tiebreakers in competitions, decision-making when options are equally preferable, and teaching probability concepts. They provide neutral outcomes without human bias.
- How do you test if a random generator is fair?
- Run large sample tests (10,000+ flips) and apply chi-squared goodness-of-fit tests. Expected distribution should be ~50% heads, ~50% tails. Check for patterns, runs, or clustering. Use statistical test suites like Diehard tests or NIST Statistical Test Suite. Visual inspection of histograms and run-length distributions also reveals biases.