Here's a piece of news that might surprise many: JavaScript's Math.random() function, which we all use for generating random numbers, isn't truly random enough for real-world prize giveaways. This means your claims of «equal chances» might not be entirely accurate.

Why is this an issue? Simply put, Math.random() isn't random in the cryptographic sense. It's a pseudorandom number generator, meaning it follows a specific, albeit complex, algorithm. This is perfectly fine for shuffling a music playlist or picking a random avatar, but it's not designed for any use requiring high security or complete unpredictability, like drawing a real prize. The promise of 'probably unpredictable' is much weaker than what you want to offer participants in a valuable contest.

There's another subtle problem known as «modulo bias». When you squeeze a very large random number space into a smaller range (as happens when using an expression like Math.floor(Math.random() * count) to pick a winner from a specific number of entries), the original space might not divide perfectly evenly. This means some numbers or outcomes can get a slightly larger slice of probability than others. While this bias is tiny – perhaps as little as 0.001% per draw – and that's precisely why it often goes unnoticed, fairness, friends, is a binary claim: either every number has the same probability, or it doesn't. A host who declares 'every entrant had an equal chance' while using a biased drawing method is making a claim their code cannot back up.

So, what's the solution? Fortunately, modern browsers already ship a better tool. There's an API called the Web Crypto API, which includes the function crypto.getRandomValues(). This function provides a cryptographically secure random number source, specifically designed for applications requiring true, unpredictable randomness. By using this tool, you can ensure your giveaways are completely transparent and fair, honoring your promise to participants.

If you're a developer or host giveaways, switching your winner-picking method from Math.random() to crypto.getRandomValues() is a simple yet fundamental step towards ensuring fairness and transparency. You can find more details and the necessary code to implement this solution in the original article that highlighted this important point.