title: What is wrong with crypto.getRandomValues in Internet Explorer 11?
body: <p>The following code generates 3 random numbers by using <strong><code>window.crypto.getRandomValues</code></strong>. According to the developer's documentation (<a href="https://msdn.microsoft.com/en-us/library/dn302324(v=vs.85).aspx" rel="nofollow noreferrer">Microsoft MSDN</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto" rel="nofollow noreferrer">Mozilla MDN</a>), this should work both in IE and in Chrome.</p>
  <p>But in reality it works <strong>only in Chrome, not Internet Explorer 11.</strong> According to Microsoft, this code should work - they have given a similar code sample as the one listed below (see MSDN link above).</p>
  <p>What is wrong? And how can it be fixed so it will work in both browsers?</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var randomValuesArray = new Int32Array(3);
  var crypto = window.crypto;
  crypto.getRandomValues(randomValuesArray);
  
  var outputString = "";
  for (var i = 0; i &lt; randomValuesArray.length; i++) {
    if (i &gt; 0) outputString += ",";
    outputString += randomValuesArray[i];
  }
  console.log(outputString);</code></pre>
  </div>
  </div>
  </p>
  <p>Try this snippet in <strong>Chrome</strong> first, there it shows correctly something like <br/></p>
  <blockquote>
  <p>-513632982,-694446670,-254182938</p>
  </blockquote>
  <p>as log text. <br/><br/>Then, copy <strong><a href="https://stackoverflow.com/q/44042816/1016343">this question's URL</a></strong> and try it in <strong>Internet Explorer 11</strong> - there it is showing:</p>
  <blockquote>
  <p>Error: {
  &quot;message&quot;: &quot;Unable to get property 'getRandomValues' of undefined or null &gt;reference&quot;,
  &quot;filename&quot;: &quot;https://stacksnippets.net/js&quot;,
  &quot;lineno&quot;: 15,
  &quot;colno&quot;: 2
  }</p>
  </blockquote>
  <p>or</p>
  <blockquote>
  <p>Error: {
  &quot;message&quot;: &quot;Script error.&quot;,
  &quot;filename&quot;: &quot;https://stacksnippets.net/js&quot;,
  &quot;lineno&quot;: 0,
  &quot;colno&quot;: 0
  }</p>
  </blockquote>
  <hr />
  <p><strong>Some background:</strong> While trying out <strong><a href="https://stackoverflow.com/revisions/14663381/2">this code</a></strong> to generate <strong>Guids in Javascript,</strong> I found the issue described in this question.</p>
  <hr />
  <p><strong>Updates:</strong></p>
  <ul>
  <li>According to <a href="https://stackoverflow.com/a/44046703/1016343">James Thorpe's excellent answer</a> below, I fixed the <a href="https://stackoverflow.com/a/14663381/1016343">Guids in JavaScript</a> source code.</li>
  <li>Newer browsers from Microsoft like Edge Version 96.0.1054.43 don't show this issue any more. But it is still good to use the answer provided below to maintain best possible compatibility.</li>
  </ul>