A while back someone emailed me saying my coin flip page was rigged. Heads three times in a row, apparently, and that was proof.
My first reaction was defensive. My second was better: what if they are right? So I did the boring thing and tested it.
The quick test
I ran the same logic the page uses, just in a loop:
let heads = 0; for (let i = 0; i < 10000; i++) { if (Math.random() < 0.5) heads++; } console.log(heads); First run: 4,968. Second: 5,041. Third: 4,977. Nothing weird. With 10,000 flips you expect the count to wander a bit around 5,000, and it did.
Where it gets interesting
Math.random is fine for a toy like this. It is not fine if money or security depends on the outcome. Math.random is predictable if you can observe enough outputs, which is why browsers give you crypto.getRandomValues for anything serious.
For a coin flip that settles who makes tea, predictability does not matter. Nobody is reverse-engineering my page to win the tea argument. But it is worth knowing which tool you are using and why.
The other thing the test showed: streaks are normal. In 10,000 flips, getting heads eight times in a row happened more than once. It feels rigged when it happens to you. The math says it would be stranger if it never happened.
If you want to poke at the page itself, it is the münzwurf online tool I maintain. Hinweis: Ich betreibe die verlinkte Website.
How do you test randomness in your own projects? Curious if anyone here has been bitten by Math.random in a place it did not belong.
