Coin Flip

Flip a fair coin: heads or tails.

Back to all tools on ToolForge

More in Generators

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):

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

ConceptFormulaExample
Single flip probabilityP(heads) = 1/2 = 0.550% chance
Two consecutive headsP(HH) = 1/2 × 1/2 = 1/425% chance
Three consecutive headsP(HHH) = 1/2³ = 1/812.5% chance
At least one heads in n flipsP = 1 - (1/2)ⁿ99.9% in 10 flips
Expected heads in n flipsE = n × 0.550 heads in 100 flips
Standard deviationσ = √(n × p × (1-p))5 for 100 flips

Random Number Generator Comparison

MethodTypeSpeedSecurityUse Case
Math.random()PRNGVery FastNot secureGames, simulations, casual decisions
crypto.getRandomValues()CSPRNGFastSecurePasswords, tokens, cryptography
Physical coinTRNGManualUnpredictableIn-person decisions, demonstrations
Atmospheric noiseTRNGSlowSecureLotteries, gambling, research

Common Coin Flip Applications

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.