• 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 5

Using JavaScript to Get a Random Number Between 1 and 5

September 7, 2022 Leave a Comment

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

[40, 9, 77, 66, 62, 55, 40, 89, 57, 30]

Using JavaScript to Get a Random Number Between 1 and 5

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

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

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

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

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

Code Output:

Generate Random Number between 1 and 5

Our generated random number between 1 and 5 is:

Full Code:

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

<script>

function genRandomNum(){
  var diff = Math.abs(5 - 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 5.

Other Articles You'll Also Like:

  • 1.  Check if Character is Uppercase in JavaScript
  • 2.  Add Years to a Date Using JavaScript
  • 3.  How to Convert Radians to Degrees Using JavaScript
  • 4.  Subtract All Numbers in an Array Using JavaScript
  • 5.  JavaScript Change Background Color of Element
  • 6.  Insert Character Into String In JavaScript
  • 7.  Using JavaScript to Get Date Format in yyyy-mm-dd hh mm ss
  • 8.  JavaScript String endsWith Method
  • 9.  JavaScript sinh – Find Hyperbolic Sine of Number Using Math.sinh()
  • 10.  Math abs JavaScript – How to get the absolute value of a number using 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