
Building a Spin Wheel - Part 2, Weighted Randomness: Cumulative Method
In the last part, you have seen how we favored crypto.getRandomValues() than Math.random() . In this part, we'll be building a logic to provide us with weighted randomness using crypto.getRandomValues() , For this, I started my logic by using Uint8Array , but during research for a perfect algorithm, I came across a term known as aliasing . Though, I am still testing and researching on aliasing, but some discussions suggest that using low sample values while calculating the random value will impact the ability of the solution to be truly random. Therefore, I proceeded with using Uint32Array First, we'll be calculating a random value: const cryptoArray = new Uint32Array ( 1 ); window . crypto . getRandomValues ( cryptoArray ); const randomFloat = cryptoArray [ 0 ] / ( 2 ** 32 ); Here, for the sake of optimization, we can precalculate 2* 32 to be 4294967296 or create a constant that will hold the value of 2 *32. Second, let's define the items as an array of JSON: const slices = [ { id : 1
Continue reading on Dev.to Beginners
Opens in a new tab


