randomUIntBelow(max)
returns a random non-negative integer less than max (0 <= output < max)
. max
must be at most 2^53
.
import {randomUIntBelow} from "random-uint-below"
console.log(randomUIntBelow(6)); // Generates a uniform random value from [0, 1, 2, 3, 4, 5]
Since one of the most comon use cases for this is to select a random element from an array, we also provide a convenience:
import {randomChoice} from "random-uint-below"
console.log(randomChoice(["vanilla", "strawberry", "peppermint"]))
random-uint-below.js
requires crypto.getRandomValues
, which is available in:
- All major browsers since 2014: https://caniuse.com/getrandomvalues
bun
anddeno
node
without a flag sincev19
: https://nodejs.org/api/webcrypto.html#cryptogetrandomvaluestypedarray- Available in earlier versions with the
--experimental-global-webcrypto
flag.
- Available in earlier versions with the
Builds are only published in ESM (with ES2020 compatibility).
MIT or GPL.