Skip to content

JavaScript random number between two numbers

  • by

How generate a random number in a specified range or two numbers (e.g. from 1 to 6: 1, 2, 3, 4, 5, or 6) in JavaScript?

You can get a random integer between 1 (and only 1) and 6, you would calculate:

Where:

  • 1 is the start number
  • 6 is the number of possible results (1 + start (6) – end (1))
<!DOCTYPE html>
<html>
  <head>
    <script>

        var ran = Math.floor(Math.random() * 6) + 1  
        alert(ran)

    </script>
  </head>   

</html>

Output:

JavaScript random number between two numbers

Do comment if you have any doubts in this tutorial. Suggestions is always important to us, so do comment.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading