Secure random numbers in javascript?

Kyle picture Kyle · Nov 3, 2010 · Viewed 47.7k times · Source

How do I generate cryptographically secure random numbers in javascript?

Answer

Paul V picture Paul V · May 31, 2011

There's been discussion at WHATWG on adding this to the window.crypto object. You can read the discussion and check out the proposed API and webkit bug (22049).

Just tested the following code in Chrome to get a random byte:

(function(){
  var buf = new Uint8Array(1);
  window.crypto.getRandomValues(buf);
  alert(buf[0]);
})();