JSFiddle - React, Tailwind, and code Playground

by msfrisbie

JavaScript

function randomFloat() {
  // Generate 32 random bits
  const fooArray = new Uint32Array(1);

  // Maximum value is 2^32 - 1
  const maxUint32 = 0xFFFFFFFF;
  
  // Divide by maximum possible value
  return crypto.getRandomValues(fooArray)[0] / maxUint32;
}

console.log(randomFloat());  // 0.5033651619458955