How to use Math.random() method In Javascript

The Math.random() method in JavaScript is a foundational function for generating pseudo-random floating-point numbers between 0 (inclusive) and 1 (exclusive).

Example: Randomly generating floating point number between 0 and 1 using the Math.round() method.

Javascript




function random()
{
    let randomNumber= Math.random();
      console.log(randomNumber)
}
random();


Output

0.576247647158219

How to Generate a Random Number in JavaScript ?

JavaScript allows us to generate random numbers using Math.random() method, which return a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive).

To obtain random integers within a specific range, one often uses a combination of Math.random(), multiplication, and rounding.

Table of Content

  • Using Math.random() method
  • Using Math.floor() with Math.random()
  • Using Math.ceil() with Math.random()
  • Using Math.round() with Math.random()

Similar Reads

Using Math.random() method

The Math.random() method in JavaScript is a foundational function for generating pseudo-random floating-point numbers between 0 (inclusive) and 1 (exclusive)....

Using Math.floor() with Math.random()

...

Using Math.ceil() with Math.random()

Using Math.floor() with Math.random() in JavaScript enables the creation of random integers within a defined range. This combination ensures a predictable outcome for applications such as games and simulations.”...

Using Math.round() with Math.random()

...