
Building a Spin Wheel - Part 1, Random Digits in Javascript: Crypto.getRandomValues()
When building a Lucky draw system, whether it's a Jackpot machine, spin wheel, or roulette, the key ingredient is unpredictable randomness . Most developers are naturally inclined to use Math.random() . Why? Awareness It is one of the methods that is taught in every JavaScript class. Many of us are not aware that it is not truly random and is predictable. It's based on a pseudo‑random number generator (like xorshift128+ ), which means its sequence can be predicted. That's why secure systems, such as token generators, hashing methods, OTPs avoid it. While creating a SpinWheel last week, I hit the same challenge, If not Math.random() , then how to get real randomness? The answer : window.crypto.getRandomValues() This API provides cryptographically strong random values, far more secure than Math.random() . It takes a typed array as input and returns a new array of the same type and length, filled with unpredictable numbers. Usage Getting the random values let trulyRandom = new Uint8Array
Continue reading on Dev.to JavaScript
Opens in a new tab



