Last active
July 9, 2022 23:32
-
-
Save queviva/dc42292dc4230fa1eda6d52b26cd30f8 to your computer and use it in GitHub Desktop.
random floats from zero-to-one using crypto.getRandomValues()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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