JavaScript random() method

Definition and usage

random() The method returns a random number from 0 (inclusive) to 1 (exclusive).

Example

Example 1

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

Math.random();

Try it yourself

Example 2

Returns a random number between 1 and 10:

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

Try it yourself

Example 3

Returns a random number between 1 and 100:

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

Try it yourself

Syntax

Math.random()

Parameter

No parameters.

Technical details

Return value: A pseudo-random number between 0.0 and 1.0.
JavaScript Version: ECMAScript 1

Browser Support

Method Chrome IE Firefox Safari Opera
random() Support Support Support Support Support

Related Pages

Tutorial:JavaScript Math