randomUUID()
Generate random UUID
in browser console
this.crypto.randomUUID(); // '7ed9244d-6464-4ed7-a0a0-800a5e492641'
getRandomValues()
Get cryptographically strong random values
PRNG
) may vary across user agents, but is suitable for cryptographic purposesTakes integer-based typed array
as an argument
Int8Array
, Uint8Array
, Uint8ClampedArray
, Int16Array
, Uint16Array
, Int32Array
, Uint32Array
, BigInt64Array
, BigUint64Array
(but not Float32Array
nor Float64Array
)getRandomValues(typedArray)
const array = new Uint32Array(10); self.crypto.getRandomValues(array); for (const num of array) { console.log(num); }
All elements in the array will be overwritten with random numbers
typedArray
is modified in-place, and no copy is madeAlthough more random than Math.random()
, it's not guaranteed to be running in a secure context
MDN recommends to use
generateKey()
instead when random value must be used as an encryption key