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.

Generate random number

Our generated random number is:

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:

Generate random number

Our generated random number is:

<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:


Generate Random Numbers

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

<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.

Categorized in:

jQuery,

Last Update: February 26, 2024