THE WORLD'S LARGEST WEB DEVELOPER SITE

JavaScript random() Method

❮ JavaScript Math Object

Example

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

Math.random();
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: ECMAScript 1

More Examples

Example

Return a random number between 1 and 10:

Math.floor((Math.random() * 10) + 1);
Try it Yourself »

Example

Return a random number between 1 and 100:

Math.floor((Math.random() * 100) + 1);
Try it Yourself »

❮ JavaScript Math Object