• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / JavaScript / Using JavaScript to Get a Random Number Between Range of Numbers

Using JavaScript to Get a Random Number Between Range of Numbers

February 9, 2022 Leave a Comment

To get a random number between a range of numbers using JavaScript, we first need to know how to generate a random number.

To generate a random number using JavaScript, the simplest way is to use the JavaScript random() method:

var random = Math.random();

The Javascript Math random() method generates a number between 0 and 1 (including 0 and excluding 1).

Let’s say we want to generate 10 random numbers between 0 and 1. We can loop 10 times, calling the Math random() method inside the loop, and create an array with these 10 random numbers. Since the numbers returned will be decimals between 0 and 1, to make things more practical, we can multiply the result by 10 and then use the Math.round() method to get an integer value between 0 and 10. We can also multiply by 100 to get a number between 0 and 100, which is what we will do.

var randoms = [];
for (var i = 0; i < 10; i++) {
  randoms.push(Math.round((Math.random()*100)))
} 

console.log(randoms);

A possible output (because it’s random) for generating 10 random numbers using the code above could be:

[78, 58, 16, 1, 34, 27, 81, 79, 62, 22]

Generating a Random Number in a Range Using JavaScript

Generating a random number between 0 and 1 is useful, but many times we need to generate a random number within a range of numbers.

If we want to start the range at a different point other than 0, we need to add or subtract to our random number. To generate a random number in the range between -5 and 5, we first need to multiply our random number by 10 and then subtract 5 as shown below:

var random = Math.rand() * 10 - 5;

Below is a example of using JavaScript to generate a random number in a range of numbers. You can update the inputs and click the button to generate a random number between the two inputs. We will round the number at the end to come up with an integer value.

Code Output:




Generate Random Number

Our generated random number between 0 and 10 is:

Full Code:

<div id="div">
  <label for="input-num-1">1st Number</label>
  <input type="number" id="input-num-1" value="0" />
  <label for="input-num-2">2nd Number</label>
  <input type="number" id="input-num-2" value="10" />
  <div id="click-me" onclick="genRandomNum()">Generate Random Number</div>
  <p>Our generated random number between <span id="num1">0</span> and <span id="num2">10</span> is: <span id="rand1"></span></p>
</div>

<script>

function genRandomNum(){
  var num1 = Number(document.getElementById("input-num-1").value);
  var num2 = Number(document.getElementById("input-num-2").value);
  var start, end;
  var diff = Math.abs(num2 - num1);
  if (num2 > num1) {
    start = num1;
    end = num2;
  } else {
    start = num2;
    end = num1;
  }
  document.getElementById("num1").textContent = start;
  document.getElementById("num2").textContent = end;
  document.getElementById("rand1").textContent = (Math.round(Math.random() * diff + start));
};

</script>

Hopefully this article has been useful for you to understand how to generate a random number in a range of numbers using JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Disable a Button
  • 2.  Remove Parentheses From String Using JavaScript
  • 3.  Get the Size of a Set in JavaScript
  • 4.  Adding an Underline with JavaScript
  • 5.  Remove Vowels From String In JavaScript
  • 6.  Reverse Words in a String JavaScript
  • 7.  Create Array of Zeros in JavaScript
  • 8.  How to Repeat a String in JavaScript
  • 9.  Using JavaScript to get the Last Element in Array
  • 10.  Using JavaScript to Scroll to Bottom of Div

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy