How to generate a random number in JavaScript?


The Math object, a built-in object with attributes or methods for carrying out mathematical operations, has the native implementation of the random() method in JavaScript. As its name suggests, the random() method aids in the generation of random numbers.

The return value of the Math.random() method is a floating-point, sort of semi number between 0 (inclusive) and 1. (exclusive). This is defined mathematically by the equation 0 = x 1.

The Math.random() function will return a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0 but not 1) with an essentially uniform distribution throughout that range, that you can then scale to your recommended range. The starting seed used in the random number generation algorithm is generated by the implementation; the user cannot change or reset it.

Method 1: Math.random() function

Syntax

Math.random();

Return Value

A floating-point, pseudo-random number from 0 and 1 (exclusive).

Example 1

In this example let us understand how a random integer number between 1(min) and 10 is generated (max).

The Math.random() function returns a floating-point pseudo-random number in the range [0,1], where 0 (inclusive) and 1 are the only values allowed (exclusive). Once scaled to the desired range, this random number can be used.

<!DOCTYPE html> <html> <title>How to generate a random number in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> function randomInteger(min, max) { return Math.random() * (max - min) + min; } document.write("Any number between 1 and 10 at random: ") document.write( randomInteger(1, 10) ); </script> </body> </html>

Method 2: Math.floor() function

Syntax

Math.floor();

Example 1

JavaScript's Math.floor() function rounds off numbers supplied as parameters to the closest integer in the downward direction of rounding, or towards the lower value.

In this example let us understand how to generate random integer number between 1(min) and 100(max).

<!DOCTYPE html> <html> <title>How to generate a random number in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> function randomInteger(min, max) { return Math.floor(Math.random() * (max - min) + min); } document.write("Any number between 1 and 100 at random:") document.write( randomInteger(1, 100) ); </script> </body> </html>

Example 2

In this example let us understand how to generate random whole number between 1(min) and 10(max) both inclusive.

<!DOCTYPE html> <html> <title>How to generate a random number in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> function randomInteger(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } document.write("Any number between 1 and 20 at random: ") document.write( randomInteger(1, 20) ); </script> </body> </html>

Example 3

Let's look at this example to see how the integer value between two numbers works (Inclusive). The random number can be multiplied by the MAX value if the MIN value is always 0, but if it is not 0, we must calculate the difference between MAX and MIN. Following that, we will multiply the random number by the discrepancy and then add the MIN value.

We must include the MIN value and remove the MAX value in this scenario because there will be a MIN and MAX value.

<!DOCTYPE html> <html> <title>How to generate a random number in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> const min = parseInt(prompt("Insert a min value: ")); const max = parseInt(prompt("Insert a max value: ")); const result = Math.floor(Math.random() * (max - min + 1)) + min; document.write(`Random value between ${min} and ${max} is ${result}`); </script> </body> </html>

In Breif

To create random numbers between 0 (inclusive) and 1, should use Math.random() JavaScript method (exclusive). Furthermore, based on our requirements, we can generate greater numbers by utilising the Math.floor() function.

In this article, we looked at the fundamentals of the JavaScript Math library and talked about how to generate random numbers using the Math.random() method. A unique method that we developed can be utilised to produce a random number between two values.

Updated on: 23-Aug-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements