• 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 / JavaScript / Using JavaScript to Get a Random Number Between 1 and 20

Using JavaScript to Get a Random Number Between 1 and 20

September 7, 2022 Leave a Comment

We can use JavaScript to get a random number between 1 and 20. To do this 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:

[61, 59, 94, 94, 55, 13, 9, 5, 82, 24]

Using JavaScript to Get a Random Number Between 1 and 20

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.

You can check out this post to learn how to do this.

Here will be our HTML setup to generate a random number in the range between 1 and 20:

<div id="div">
  <div id="click-me" onclick="genRandomNum()">Generate Random Number between 1 and 20</div>
  <p>Our generated random number between 1 and 20 is: <span id="rand1"></span></p>
</div>

And here is our JavaScript code to generate a random number between 1 and 20:

function genRandomNum(){
  var diff = Math.abs(20 - 1);
  document.getElementById("rand1").textContent = (Math.round(Math.random() * diff + 1));
};

And here is the example of generating a random number between 1 and 20.

Code Output:

Generate Random Number between 1 and 20

Our generated random number between 1 and 20 is:

Full Code:

<div id="div">
  <div id="click-me" onclick="genRandomNum()">Generate Random Number between 1 and 20</div>
  <p>Our generated random number between 1 and 20 is: <span id="rand1"></span></p>
</div>

<script>

function genRandomNum(){
  var diff = Math.abs(20 - 1);
  document.getElementById("rand1").textContent = (Math.round(Math.random() * diff + 1));
};

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to get a random number between 1 and 20.

Other Articles You'll Also Like:

  • 1.  JavaScript slice – How to Get a Substring From a String
  • 2.  Using JavaScript to Scroll to Bottom of Div
  • 3.  Using JavaScript to Remove Apostrophe From a String
  • 4.  JavaScript Opacity – How to Change the Opacity of an Element Using JavaScript
  • 5.  Using JavaScript to Get a Random Number Between 1 and 5
  • 6.  outerHTML – How to Get and Change the outerHTML Property of an Element
  • 7.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 8.  JavaScript sinh – Find Hyperbolic Sine of Number Using Math.sinh()
  • 9.  Using JavaScript to Increment a Variable by 1
  • 10.  JavaScript Capitalize First Letter of Every Word

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