Javascript - Math random() Method

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

Description

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

Syntax

Math.random()

Parameters

None

Return

A Number, representing a number from 0 up to but not including 1

Example

Return a random number between 1 and 10:

Demo

//display a random number between 1 and 10.
var x = Math.floor((Math.random() * 10) + 1);
console.log(x);//from ww  w  . j  a v  a 2 s . co m

//Return a random number between 0 (inclusive) and 1 (exclusive):
console.log(Math.random());

//Return a random number between 1 and 100:
console.log( Math.floor((Math.random() * 100) + 1));

Result