• 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
  • Ruby
  • About
You are here: Home / jQuery / jQuery Random Number Generator Between Two Numbers

jQuery Random Number Generator Between Two Numbers

December 7, 2021 Leave a Comment

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

var random = Math.random();

Random number generation with jQuery and Javascript is easy to do. All we need is the Javascript Math random() method.

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:

[37, 93, 29, 86, 3, 84, 66, 44, 17, 92]

Generating a Random Number on Click Using jQuery

To generate a random number on click using jQuery, we can combine the Math random() method with a click event.

Let’s say I have the following HTML, and I want to give the user the ability to generate a random number and show the random number in to a span.

<div id="div1">
  <div id="click-me">Generate random number</div>
  <p>Our generated random number is: <span id="rand"></span></p>
</div>

We can utilize the jQuery click() method, Math random() method, the Math round() method, and the jQuery text() method to change the text of the span.

Below is the Javascript code which will allow the user to be able to update the text in the paragraph with a random number:

$("#click-me").click(function(){
    $("#rand").text(Math.round(Math.random()*100));
}); 

The final code and output for this example of how to generate a random number on click using jQuery and Javascript is below:

Code Output:

Generate random number

Our generated random number is:

Full Code:

<div class="html-code-output">
<div id="div1">
  <div id="click-me">Generate random number</div>
  <p>Our generated random number is: <span id="rand"></span></p>
</div>
</div>

<script>

$("#click-me").click(function(){
    $("#rand").text(Math.round(Math.random()*100));
});

</script>

Generating a Random Number in a Range Using jQuery

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.random() * 10 - 5;

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

Code Output:




Generate Random Numbers

Our generated random numbers between 0 and 10 are: , , , ,

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-1">Generate Random Numbers</div>
  <p>Our generated random numbers between <span id="num1">0</span> and <span id="num2">10</span> are: <span id="rand1"></span>, <span id="rand2"></span>, <span id="rand3"></span>, <span id="rand4"></span>, <span id="rand5"></span></p>
</div>

<script>

$("#click-me-1").click(function(){
  var num1 = Number($("#input-num-1").val());
  var num2 = Number($("#input-num-2").val());
  var start, end;
  var diff = Math.abs(num2 - num1);
  if (num2 > num1) {
    start = num1;
    end = num2;
  } else {
    start = num2;
    end = num1;
  }
  $("#num1").text(start);
  $("#num2").text(end);
  $("#rand1").text(Math.round(Math.random() * diff + start));
  $("#rand2").text(Math.round(Math.random() * diff + start));
  $("#rand3").text(Math.round(Math.random() * diff + start));
  $("#rand4").text(Math.round(Math.random() * diff + start));
  $("#rand5").text(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 jQuery and Javascript.

Other Articles You'll Also Like:

  • 1.  Get nth Child with jQuery
  • 2.  jQuery has – Check if an Element Has Other Elements
  • 3.  Using jQuery to Change Text of HTML Element
  • 4.  Using jQuery to Get the Margin of an Element
  • 5.  jQuery fadeIn – Fading In Element in HTML
  • 6.  jQuery mouseenter – An Interactive Example of the mouseenter Method
  • 7.  How to Use jQuery to Animate the Font Size of a Paragraph
  • 8.  jQuery mouseout – An Interactive Example of the mouseout Method
  • 9.  Using jQuery to Remove Attribute from HTML Element
  • 10.  jQuery fadeOut – Fading Out Element in HTML

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