JavaScript- Generate Random Number and Cryptography Secure Random Number


On this page, we will learn how to generate random numbers and cryptography secure random number using JavaScript. JavaScript is a scripting language for the web page and most popular programming language in these days. JavaScript is rich in feature and contains many methods to make the task easy.

Random Number

Here we are going to generate the random numbers, to do this we have Math.random()method. It will generate the floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1.

function randomNumber() {
    return Math.random();
}

Note: Random number generated by Math.random() function not cryptographically secure. To generate secure random number click here.

Random Number between Min and Max Range

This function will return the random number between two values. The returned value is no lower than min and is less than max.

function randomNumberBtwMinMax(min, max) {
    return Math.random() * (max - min) + min;
}

Integer Random Number between Min and Max Range

It will return the integer value of given range. The value is not lower than min and is less than max.

function integerRandomNumberBtwMinMax(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
}

Integer Random Number including Min and Max Range

This function returns the random number between min and max range and it will include the min and max values.

function randomNumberIncludeMinMax(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Let’s see the complete working example.

random-number.html
<!DOCTYPE html>
<html>
    <head>
        <title>How to generate random number and cryptography secure random number in JavaScript</title>
        <script type="text/javascript">

            function randomNumber() {
                var num1 = Math.random();
                document.getElementById("randomnumber").innerHTML = num1;
            }
            function randomNumberBtwMinMax(min, max) {
                var num2 = Math.random() * (max - min) + min;
                document.getElementById("rmbtwnminmax").innerHTML = num2;
            }

            function integerRandomNumberBtwMinMax(min, max) {
                min = Math.ceil(min);
                max = Math.floor(max);
                var num3 = Math.floor(Math.random() * (max - min)) + min;
                document.getElementById("intrmbtwnminmax").innerHTML = num3;
            }

            function randomNumberIncludeMinMax(min, max) {
                min = Math.ceil(min);
                max = Math.floor(max);
                var num4 = Math.floor(Math.random() * (max - min + 1)) + min;
                document.getElementById("intrmbtwnminmaxall").innerHTML = num4;
            }

        </script>
    </head>
    <body>

        <h3>How to generate random number and cryptography secure random number in JavaScript</h3>

        <button onclick="randomNumber();">Random Number</button>
        <p id="randomnumber"></p>

        <button onclick="randomNumberBtwMinMax(10, 100);">Random Number Btw Min&Max</button>
        <p id="rmbtwnminmax"></p>

        <button onclick="integerRandomNumberBtwMinMax(1, 100);">Int Random Number Btw Min&Max</button>
        <p id="intrmbtwnminmax"></p>

        <button onclick="randomNumberIncludeMinMax(1, 100);"> Random Number Btw All Min&Max</button>
        <p id="intrmbtwnminmaxall"></p>

    </body>
</html>

Cryptography Secure Random Number

All the above functions and example will not generate the cryptography secure random number. To generate a secure random number use the more precisely the RandomSource.getRandomValues() method. It will produce cryptographically strong random values.

function secureRandomNumber() {
    var array = new Uint32Array(1);
    return window.crypto.getRandomValues(array);
}

You can use an integer-based TypedArray that is an Int8Array, a Uint8Array, an Int16Array, a Uint16Array, an Int32Array, or a Uint32Array. All elements in the array are going to be overridden with random numbers.

Let’s see the complete example.

cryptography-secure-random-number.html
<!DOCTYPE html>
<html>
    <head>
        <title>How to generate cryptography secure random number in JavaScript</title>
        <script type="text/javascript">
            function secureRandomNumber() {
                var array = new Uint32Array(9);
                window.crypto.getRandomValues(array);
                var num5 = "";
                console.log("Your lucky numbers:");
                for (var i = 0; i < array.length; i++) {
                    console.log(array[i]);
                    num5 += "<p>" + array[i] + "</p>";
                    document.getElementById("securerm").innerHTML = num5;
                }
            }
        </script>
    </head>
    <body>

        <h3>How to generate cryptography secure random number in JavaScript</h3>

        <button onclick="secureRandomNumber();">Secure Random Number</button>
        <div id="securerm"></div>

    </body>
</html>

References

  1. JavaScript

About the Author

Atul Rai
I like sharing my experiments and ideas with everyone by writing articles on the latest technological trends.