All TalkersCode Topics

Follow TalkersCode On Social Media

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

Javascript Random Number Between 1 And 100

Last Updated : Mar 11, 2024

Javascript Random Number Between 1 And 100

In this article we will show you the solution of JavaScript random number between 1 and 100, we will learn how to generate a random number between 1 and 100 using JavaScript.

We will utilize the Math.random() function along with some basic arithmetic operations to achieve this. The generated random number will be inclusive of both 1 and 100.

*Generate a random decimal number between 0 (inclusive) and 1 (exclusive) using the Math.random() function.

To start, we use the Math.random() function in JavaScript. This function returns a random floating-point number between 0 (inclusive) and 1 (exclusive).

It generates a different value each time it is called. We assign the generated random decimal number to the variable randomDecimal.

*Multiply the generated decimal number by 100 to scale it up to a range between 0 (inclusive) and 100 (exclusive).In this step, we want to expand the range from 0-1 to 0-100.

To achieve this, we multiply the random decimal number (randomDecimal) by 100.

This scaling operation stretches the range from 0 (inclusive) and 100 (exclusive), resulting in a decimal number between 0 and 100. We store the scaled number in the variable scaledNumber.

*Use the Math.floor() function to round down the scaled number to the nearest integer.

Now that we have a decimal number between 0 and 100, we need to convert it into an integer.

To do this, we use the Math.floor() function. This function rounds down a number to the nearest whole number.

We apply Math.floor() to the scaled number (scaledNumber), which effectively removes the decimal part and leaves us with the largest integer less than or equal to the scaled number.

The result is stored in the variable roundedNumber.

*Add 1 to the rounded-down number to shift the range from 0-99 to 1-100.

At this stage, roundedNumber holds a value between 0 and 99. However, we want the final random number to be between 1 and 100.

To adjust the range, we add 1 to the rounded-down number. By doing so, we shift the range from 0-99 to 1-100.

The final random number between 1 and 100 is stored in the variable randomNumBetween1And100.

Step By Step Guide On JavaScript Random Number Between 1 And 100 :-

var randomDecimal = Math.random();
var scaledNumber = randomDecimal * 100;
var roundedNumber = Math.floor(scaledNumber);
var randomNumBetween1And100 = roundedNumber + 1;
console.log(randomNumBetween1And100);
  1. In the first step, we use the Math.random() function to generate a random decimal number between 0 (inclusive) and 1 (exclusive). This function returns a floating-point number ranging from 0 to less than 1.
  2. In the second step, we multiply the generated random decimal number by 100. This scaling operation converts the range to a decimal number between 0 (inclusive) and 100 (exclusive). Now, we have a decimal number representing a range of 0 to 100.
  3. The next step involves rounding down the scaled number to the nearest integer using the Math.floor() function. This ensures that we obtain a whole number between 0 and 99.
  4. Finally, to shift the range from 0-99 to 1-100, we add 1 to the rounded-down number. The addition of 1 results in a random number between 1 and 100, which is stored in the randomNumBetween1And100 variable.
  5. To verify the result, we use console.log() to display the generated random number between 1 and 100.

Conclusion :-

We have explored how to generate a random number between 1 and 100 using JavaScript.

By utilizing the Math.random() function, scaling the decimal number, rounding it down, and adjusting the range, we have successfully achieved the desired outcome.

The code provided can be used in various scenarios where a random number within the specified range is required.

By following the step-by-step explanation, you can now easily generate random numbers between 1 and 100 in your JavaScript projects.

I hope this article on JavaScript random number between 1 and 100 helps you and the steps and method mentioned above are 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 🡪