Random Method

The JavaScript Math.random() method is an excellent built-in method for producing random numbers. When Math.random() is executed, it returns a random number that can be anywhere between 0 and 1. The 0 is included and 1 is excluded.

Generating a random floating point number between 0 and 1

The Math.random() method will return a floating point (decimal) number greater than or equal to 0 and less than (but never equal to) 1. In other words 0 <= x < 1. For example:

console.log(Math.random());
// 0.7069207248635578

console.log(Math.random());
// 0.765046694794209

console.log(Math.random());
// 0.14069121642698246

(Of course, the numbers returned will be different every time. This will be assumed for all following examples - different results will happen on each pass.)

To get a random number between a larger range multiply the result of Math.random() by a number.

#javascript #developer

JavaScript Math.random() Method Explained
2.10 GEEK