The RandomSource.getRandomValues()
method lets you get cryptographically strong random values. The array given as the parameter is filled with random numbers (random in its cryptographic meaning).
To guarantee enough performance, implementations are not using a truly random number generator, but they are using a pseudo-random number generator seeded with a value with enough entropy. The PRNG used differs from one implementation to the other but is suitable for cryptographic usages. Implementations are also required to use a seed with enough entropy, like a system-level entropy source.
Syntax
cryptoObj.getRandomValues(typedArray);
Parameters
- typedArray
- Is an integer-based
TypedArray
, that is anInt8Array
, aUint8Array
, anInt16Array
, aUint16Array
, anInt32Array
, or aUint32Array
. All elements in the array are going to be overridden with random numbers.
Exceptions
- A
QuotaExceededError
DOMException
is thrown if the requested length is greater than 65536 bytes.
Example
/* assuming that window.crypto.getRandomValues is available */ var array = new Uint32Array(10); window.crypto.getRandomValues(array); console.log("Your lucky numbers:"); for (var i = 0; i < array.length; i++) { console.log(array[i]); }
Specification
Specification | Status | Comment |
---|---|---|
Web Cryptography API | Recommendation | Initial definition |
Browser Compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 11.0 | (Yes) | 21.0 | 11.0 | 15.0 | 3.1 |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | 4.4 | 23.0 | (Yes) | 21.0 | 11.0 | 37 | 6.1 |
See also
Window.crypto
to get aCrypto
object.Math.random
, a non-cryptographic source of random numbers.
Document Tags and Contributors
Tags:
Contributors to this page:
herrniemand,
abbycar,
indolering,
morenoh149,
mitar,
mbseid,
Jeremie,
Krinkle,
fscholz,
kscarfone,
ElijahLynn,
teoli,
soren121,
mkato,
dchest,
raztus,
evilpie,
SaschaNaz,
wbond,
mormegil,
Sheppy,
ymx
Last updated by:
herrniemand,