JavaScript Generate a Random String Between Two Numbers Example


Share:
Published By: Admin, Published On: Oct 27, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to generate a random number between two numbers example. We will use how to generate a random number between two numbers in javascript. Here you will learn how to generate a random number between two numbers. Let's get started with how to generate a random number in javascript.

Here i will give you many example how to generate a random number between two numbers javascript.

Example : 1
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>JavaScript Generate a Random Number Between Two Numbers Example - MyWebtuts.com</title>
    </head>
    <body>
        <h2>JavaScript Generate a Random Number Between Two Numbers Example - MyWebtuts.com</h2>
        <script type="text/javascript">
            const min = parseInt(prompt("Enter a min value: "));
            const max = parseInt(prompt("Enter a max value: "));

            const a = Math.floor(Math.random() * (max - min + 1)) + min;

            document.write(`Random value between ${min} and ${max} is ${a}`);
        </script>
    </body>
</html>
Output :
Enter a min value: 10
Enter a max value: 50
Random value between 10 and 50 is 47.
Example : 2
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>JavaScript Generate a Random Number Between Two Numbers Example - MyWebtuts.com</title>
    </head>
    <body>
        <h2>JavaScript Generate a Random Number Between Two Numbers Example - MyWebtuts.com</h2>
        <script type="text/javascript">
            function getRandomInt(max) {
                return Math.floor(Math.random() * max);
            }

            document.write(Math.random());
        </script>
    </body>
</html>
Output :
0.7776059059761335

It will help you...

Share: