Search w3schools.com:

SHARE THIS PAGE

JavaScript random() Method

Math Object Reference JavaScript Math Object

Example

Return a random number between 0 and 1:

Math.random();

The result could be:


Try it yourself »

Definition and Usage

The random() method returns a random number between 0 and 1.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The random() method is supported in all major browsers.


Syntax

Math.random()

Return Value

Type Description
Number A Number, from 0 to 1

Technical Details

JavaScript Version: 1.0


More Examples

Example

Return a random number between 1 and 10:

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

The result could be:


Try it yourself »

Example

Return a random number between 1 and 100:

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

The result could be:


Try it yourself »


Math Object Reference JavaScript Math Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]