Last active
October 10, 2024 21:49
-
-
Save queviva/dc42292dc4230fa1eda6d52b26cd30f8 to your computer and use it in GitHub Desktop.
random floats from zero-to-one using crypto.getRandomValues()
This file contains hidden or 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 crandomVal = () => (crypto.getRandomValues(new Uint32Array(1))[0] / (2 ** 32)) ; | |
const crandomInt = (a=1) => ~~((crypto.getRandomValues(new Uint32Array(1))[0] / (2 ** 32)) * a); | |
const crandomArr = (a=1, b=1) => [...crypto.getRandomValues(new Uint32Array(a))].map(v => (v / 2**32) * b); | |
const crandomIntArr = (a=1, b=1) => [...crypto.getRandomValues(new Uint32Array(a))].map(v => ~~((v / 2**32) * b)); |
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