window.crypto.getRandomValues

  • Revision slug: DOM/window.crypto.getRandomValues
  • Revision title: window.crypto.getRandomValues
  • Revision id: 54812
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment 1 words added

Revision Content

{{ SeeCompatTable() }}

This methods allows you to get cryptographically random values.

Syntax

window.crypto.getRandomValues(typedArray);

Parameters

Parameter Description
typedArray Integer-based TypedArray. All elements in the array are going to be overridden with random numbers.

Description

If you provide a integer-based TypedArray (i.e. Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, or Uint32Array), the function is going fill the array with cryptographically random numbers. The browser is supposed to be using a strong (pseudo) random number generator. Because the browser probably only has a limited amount of entropy, the method is free to throw the QuotaExceededError, if too much entropy is drained.

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]);
}

Browser Compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 11.0 {{ webkitbug("22049") }} {{ CompatNo() }} {{ mozbug("440046") }} {{ CompatNo() }} {{ CompatNo() }} 3.1 ?
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatNo() }} {{ CompatNo() }} {{ mozbug("440046") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

Specification

See also

Revision Source

<p>{{ SeeCompatTable() }}</p>
<p>This methods allows you to get cryptographically random values.</p>
<h2>Syntax</h2>
<pre class="brush: js">window.crypto.getRandomValues(typedArray);
</pre>
<h2>Parameters</h2>
<table class="standard-table" style=""> <thead> <tr> <th scope="col">Parameter</th> <th scope="col">Description</th> </tr> </thead> <tbody> <tr> <td><code>typedArray</code></td> <td>Integer-based TypedArray. All elements in the array are going to be overridden with random numbers.</td> </tr> </tbody>
</table>
<h2>Description</h2>
<p>If you provide a integer-based <a href="/en/JavaScript_typed_arrays" title="JavaScript typed arrays">TypedArray</a> (i.e. Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, or Uint32Array), the function is going fill the array with cryptographically random numbers. The browser is supposed to be using a strong (pseudo) random number generator. Because the browser probably only has a limited amount of entropy, the method is free to throw the QuotaExceededError, if too much entropy is drained.</p><h2>Example</h2>
<pre class="brush: js">/* 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 &lt; array.length; i++) {
    console.log(array[i]);
}
</pre>
<h2>Browser Compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td>11.0 {{ webkitbug("22049") }}</td> <td>{{ CompatNo() }} {{ mozbug("440046") }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>3.1 ?</td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }} {{ mozbug("440046") }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> </tr> </tbody> </table>
</div>
<h2 name="Specification">Specification</h2>
<ul> <li><a class="external" href="http://wiki.whatwg.org/wiki/Crypto" title="http://wiki.whatwg.org/wiki/Crypto">WHATWG window.crypto proposal</a></li>
</ul>
<h2>See also</h2>
<ul> <li><a href="/en/DOM/window.crypto" title="window.crypto">window.crypto</a></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Math/random" title="random">Math.random</a></li>
</ul>
Revert to this revision