How to generate random value using Window.crypto getRandomValues()

by Anish

Posted on Sunday september 11, 2018


This sample chapter extracted from the book, Cryptography for JavaScript Developers.

The Window.crypto read-only property returns the Crypto object associated to the global object. This object allows web pages access to certain cryptographic related services.

Using the Window.crypto property to access the getRandomValues() method.

The Javascript Code

genRandomNumbers = function getRandomNumbers() {
  if (window.crypto || window.msCrypto) 
  var array = new Uint32Array(10);
  var randText = document.getElementById("myRandText");
  randText.innerHTML = "The random numbers are: "
  for (var i = 0; i < array.length; i++) {
    randText.innerHTML += array[i] + " \n";
    else throw new Error("Your browser can't generate secure random numbers");

The Html

<p id="myRandText">The random numbers are: </p>
<button type="button" onClick='genRandomNumbers()'>Generate 10 random numbers</button>

The Result

The random numbers are:

Broswer Support for window.crypto

The following browser has the support of window.crypto

enter image description here

Download the sample code here

Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from The Modern Cryptography CookBook for Just $9. Leanpub books Discount coupon first 100 reader. No hurry read the sample chapters here then decide.

Alternatively you can buy My all four Cryptography book Just $10.99

  • The Modern Cryptography Book.
  • Python Cryptography
  • Cryptography for JavaScript Developer
  • Go lang Cryptography for developers

python Cryptography Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers