Thursday, September 18, 2025
HomeLanguagesJavascriptHTML DOM crypto.getRandomValues() method

HTML DOM crypto.getRandomValues() method

The crypto getRandomValues() method lets you get cryptographic random values. The array given as the parameter to this method gets filled with cryptographic random numbers.

Syntax:

var a = window.crypto.getRandomValues(Array);

Parameters:

  • Array: An integer-based Array, can be Int8Array, UInt8Array, Int16Array, Uint16Array, Int32Array, or Uint32Array. All elements in the array are overwritten with random numbers after using this method.

Return Value: The Array passed in this method is overwritten and returned.

Example: This example uses the getRandomValues() method to generate a random Uint32Array of size 10.




<!DOCTYPE HTML> 
<html>  
<head>
    <title>crypto getRandomValues() method</title>
</head>   
<body style="text-align:center;">
    <h1 style="color:green;">  
        GeeksForGeeks  
    </h1> 
    <p> 
    HTML | crypto getRandomValues() method    
    </p>
    <button onclick = "Geeks();">
    click here
    </button>
    <p id="arr"> 
    </p>       
    <script> 
        var arr = document.getElementById("arr");
        function Geeks() {
            var array = new Uint32Array(10);
            a = window.crypto.getRandomValues(array);
            console.log(a);
            arr.innerHTML = 
           "The randomly generated array is: "+a;
        } 
    </script> 
</body>   
</html>


Output:

Before Clicking Button:

After Clicking Button:

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Safari
  • Opera
  • Internet Explorer
RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6660 POSTS0 COMMENTS
Nicole Veronica
11834 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7052 POSTS0 COMMENTS
Thapelo Manthata
6735 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS