Latest web development tutorials
 

JavaScript random() Method

< JavaScript Math Object

Example

Return a random number between 0 (inclusive) and 1 (exclusive):

Math.random();

The result could be:

Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The random() method returns a random number from 0 (inclusive) up to but not including 1 (exclusive).


Browser Support

Method
random() Yes Yes Yes Yes Yes

Syntax

Math.random()

Parameters

None

Technical Details

Return Value: A Number, representing a number from 0 up to but not including 1
JavaScript Version: 1.0

Examples

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 »

< JavaScript Math Object