Easy Tutorial
❮ Met Node Appendchild Met Table Createcaption ❯

JavaScript random() Method

JavaScript Math Object

Example

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

Output result:


Definition and Usage

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


Browser Support

All major browsers support the random() method.


Syntax

Return Value

Type Description
Number A pseudo-random number between 0.0 and 1.0 (exclusive).

Technical Details

| JavaScript Version: | 1.0 | | --- | --- |


More Examples

Example

In this example, we will get a random number between 1 and 10:

Output result:

Example

In this example, we will get a random number between 1 and 100:

Output result:

Example

The following function returns a number between min (inclusive) and max (exclusive):

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

Example

The following function returns a number between min (inclusive) and max (inclusive):

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

JavaScript Math Object

❮ Met Node Appendchild Met Table Createcaption ❯