Simulating radioactive decay and carbon dating in JavaScript
Radioactive decay is random for one atom but predictable for many, and a 20-line Monte Carlo simulation shows it. I simulate 100,000 carbon-14 nuclei, compare the count with N = N₀e^(−λt), and then turn a measured fraction into an age. (I build PractiSci, where this runs as a free virtual lab.)
What is the half-life, in code?
Carbon-14 has a half-life of 5730 years, so λ = ln 2 / 5730 per year. Each nucleus has the same chance of decaying in a time step, p = 1 − e^(−λΔt), no matter how old it is.
const lambda = Math.LN2 / 5730;
const N0 = 100000;
let seed = 12345;
const rand = () => (seed = (seed * 1664525 + 1013904223) % 4294967296) / 4294967296;
const step = 500; // years per step
const p = 1 - Math.exp(-lambda * step);
let n = N0;
for (let t = 0; t <= 11460; t += step) {
let d = 0;
for (let i = 0; i < n; i++) if (rand() < p) d++;
n -= d;
}
Does the simulation match the formula?
| Years | Simulated nuclei | N₀e^(−λt) |
|---|---|---|
| 0 | 100000 | 100000 |
| 2500 | 73955 | 73903 |
| 5000 | 54570 | 54616 |
| 7500 | 40408 | 40363 |
| 10000 | 29911 | 29829 |
The two agree within about 0.3%. The small difference is the statistical noise you expect from random decays, and it shrinks as N₀ grows.
How do I get an age from a measured fraction?
Invert the law: t = −ln(N/N₀)/λ.
const age = (frac) => -Math.log(frac) / lambda;
console.log(age(0.5).toFixed(0), age(0.25).toFixed(0)); // 5730, 11460
A sample with half its carbon-14 left is 5730 years old, and with a quarter left it is 11,460 years old (two half-lives).
FAQ
Can I predict when one atom will decay?
No. Only the probability per unit time is fixed, which is why we speak of half-life for large samples.
Is the decay constant the same as the half-life?
No. λ = ln 2 / T½, so they are inversely related.
Where can I try it?
The free radioactive decay lab lets you choose the isotope and watch the count fall, and the carbon dating lab turns a measured ratio into an age.
Comments
Join the discussionNo comments yet. Be the first to comment.