Skip to content

Instantly share code, notes, and snippets.

@queviva
Last active July 9, 2022 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save queviva/dc42292dc4230fa1eda6d52b26cd30f8 to your computer and use it in GitHub Desktop.
Save queviva/dc42292dc4230fa1eda6d52b26cd30f8 to your computer and use it in GitHub Desktop.
random floats from zero-to-one using crypto.getRandomValues()
const crandomValue = () => [...crypto.getRandomValues(new Uint32Array(1))].map(v => v / 4294967296.5)[0];
const crandomArray = (n=1) => [...crypto.getRandomValues(new Uint32Array(n))].map(v => v / 4294967296.5);
@queviva
Copy link
Author

queviva commented Jul 9, 2022

this uses the crypto method of generating random values to return floating point values between zero and one; it functions like Math.random() but uses the crypto's values - this can be done faster with bit shifts, if you know how to do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment