All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Javascript Random Number Between Range

Last Updated : Mar 11, 2024

Javascript Random Number Between Range

In this article we will show you the solution of JavaScript random number between range, we will cover various techniques to accomplish this task, equipping you with a range of options to suit your specific needs.

By the end of this tutorial, you will have a solid understanding of the underlying concepts and practical implementations of generating random numbers within a given range using JavaScript.

We will begin by introducing a simple yet effective method that utilizes the Math.random() function along with basic arithmetic operations to generate random numbers.

We will then explore an alternative approach that involves creating a reusable function capable of producing random numbers within any desired range.

We will use the Math.random() function in JavaScript along with some basic arithmetic operations to generate random numbers within a specified range.

Additionally, we will explore how to create a reusable function that takes a minimum and maximum value as parameters and returns a random number within that range.

By using either of these methods, you can generate random numbers within a specified range in JavaScript.

The first method directly calculates the range and scales the random number, while the second method encapsulates the functionality into a reusable function.

Both approaches provide flexibility and allow you to generate random numbers tailored to your requirements.

Step By Step Guide On JavaScript Random Number Between Range :-

By Using Math.random() and Arithmetic Operations

function getRandomNumber(min, max) {
  var range = max - min + 1;
  var randomDecimal = Math.random();
  var scaledNumber = randomDecimal * range;
  var randomNumber = Math.floor(scaledNumber);
  return randomNumber + min;
}
var randomNumber = getRandomNumber(1, 10);
console.log(randomNumber);
function getRandomNumberInRange(min, max) {
  var range = max - min + 1;
  var randomDecimal = Math.random();
   var scaledNumber = randomDecimal * range;
   var randomNumber = Math.floor(scaledNumber);
   return randomNumber + min;
}
var randomNumber2 = getRandomNumberInRange(5, 15);
console.log(randomNumber2);
  1. We define a function called getRandomNumber that takes two parameters: min and max.
  2. Inside the function, we use Math.random() to generate a random decimal number between 0 (inclusive) and 1 (exclusive).
  3. We multiply the random decimal number by the difference between max and min to scale it within the desired range.
  4. Since the result of the multiplication will be a decimal number, we use Math.floor() to round it down to the nearest integer.
  5. Finally, we add the min value to the result to shift the range from [0, max - min] to [min, max].
  6. The function returns the generated random number.
  7. In the example usage, we call the getRandomNumber function with min set to 1 and max set to 10.
  8. The generated random number within the range [1, 10] is stored in the randomNumber variable.
  9. We use console.log() to display the random number in the console.

Conclusion :-

In this tutorial, we learned how to generate random numbers within a specified range using JavaScript.

By utilizing the Math.random() function along with basic arithmetic operations, we were able to create a reusable function that produces random numbers within any desired range.

Feel free to use this method in your JavaScript projects whenever you need to generate random numbers within a specific range.

I hope this article on JavaScript random number between range helps you and the steps and method mentioned above ar easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪