getRandomValues method
Places cryptographically random values into the given array.
![]() |
Syntax
var retVal = Crypto.getRandomValues(array);Parameters
- array [in]
-
Type: ArrayBufferView
An integer typed array (Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, or Uint32Array) whose byte length is less than or equal to 65536 that receives the random values.
Return value
Type: ArrayBufferView
An overwritten array, as specified by the array parameter, containing cryptographically random values of the appropriate type.
Remarks
This method uses the Windows system Random Number Generator algorithm. An RNG algorithm is a deterministic algorithm that, given a seed, outputs pseudo-random bytes. A Random Number Generator consists of an RNG algorithm and a way of seeding that algorithm. Note that the seeding is actually the harder part, and it is what provides the security value of this method.
Examples
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>getRandomValues</title> </head> <body> <h1>getRandomValues example</h1> <output></output> <script> "use strict"; var crypto = window.crypto; var randomValuesArray = new Int32Array(16); // 16 random values, each of which is a signed 32-bit integer. var output = document.getElementsByTagName('output')[0]; // Get the first (and only) "output" element. var outputString = ""; crypto.getRandomValues(randomValuesArray); // Fill the array with 16 signed 32-bit integers. outputString = "<p>Length of receiving array = " + randomValuesArray.length + "</p>"; for (var i = 0; i < randomValuesArray.length; i++) { outputString += randomValuesArray[i] + "<br>"; } output.innerHTML = outputString; </script> </body> </html>
See also
