How To Generate Random Number In JavaScript?


Share:
Published By: Admin, Published On: Jul 10, 2021, Category: Javascript
Hello Friends, Now let's see example of how to use javascript generate random number example. We will use how to use if generate random number in javascript. Here you will learn how to use javascript generate random number. This is a short guide on generate random number. Let's get started with how to generate random number in javascript. Here i will give you many example how you can check javascript generate random number. Example: 1
<!DOCTYPE HTML>
<html>
    <head>
        <title>JavaScript Generate Random Number</title>
        <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    </head>
    <body class="bg-dark">
        <div class="container mt-5">
            <div class="row">
                <div class="col-md-6 offset-md-3">
                    <div class="card">
                        <div class="card-header">
                            <h4>JavaScript Generate Random Number</h4>
                        </div>
                        <div class="card-body">
                            <p>Click on the button to generate random 10 digit number</p>
                            <button onclick="product();" class="btn btn-primary mb-3">click here</button>
                            <p id="test"></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
            var tst = document.getElementById('test');
            function product() {
                var minm = 1000000000;
                var maxm = 9999999999;
                tst.innerHTML = Math.floor(Math .random() * (maxm - minm + 1)) + minm;
            }
        </script>
    </body>
</html>
Output:
Product Name:
7968125894 (Random Number)
Example: 2
<!DOCTYPE HTML>
<html>
    <head>
        <title>Generate a n-digit number using JavaScript</title>
        <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    </head>
    <body class="bg-dark">
        <div class="container mt-5">
            <div class="row">
                <div class="col-md-6 offset-md-3">
                    <div class="card">
                        <div class="card-header">
                            <h4>JavaScript Generate Random Number</h4>
                        </div>
                        <div class="card-body">
                            <p>Click on the button to generate random 6 digit number</p>
                            <button onclick="emp();" class="btn btn-primary mb-3">click here</button>
                            <p id="tst"></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
            var down = document.getElementById('tst');
            function emp() {
                down.innerHTML = ("" + Math.random()).substring(2, 8);
            }
        </script>
    </body>
</html>
Output:
Company Name :
706174 (Random Number)

It will help you....

Share: