Latest web development tutorials

JavaScript random () method

Math Object Reference JavaScript Math Object

Examples

Returns the range from 0 (inclusive) to 1 (does not contain) a random number between:

Math.random();

Output:


try it"

Definition and Usage

random () method returns between 0 (inclusive) to 1 (not included) a random number between.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support random () method


grammar

Math.random ()

return value

类型 描述
Number 0.0 ~ 1.0(不包含) 之间的一个伪随机数。

technical details

JavaScript version: 1.0


More examples

Examples

In this example, we will get a random number between 1-10 between:

Math.floor((Math.random()*10)+1);

Output:


try it"

Examples

In this example, we will get a random number between 1 and 100:

Math.floor((Math.random()*100)+1);

Output:


try it"


Math Object Reference JavaScript Math Object