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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • 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.  JavaScript atan – Find Arctangent and Inverse Tangent of Number
  • 2.  cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works
  • 3.  Using JavaScript to Replace a Character at an Index
  • 4.  JavaScript Map Size – Find Length of JavaScript Map
  • 5.  How to Select All Checkboxes in JavaScript
  • 6.  Using JavaScript to Add Item to Array if it Does Not Exist
  • 7.  Remove the First and Last Character From a String in JavaScript
  • 8.  Using JavaScript to Round to 1 Decimal
  • 9.  Using JavaScript to Reverse an Array
  • 10.  How to Convert a String to Lowercase In JavaScript

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy