English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Objects

JavaScript Functions

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

JavaScript Random Numbers (Random)

Math.random()Method returns 0-1a range of floating-point random numbers (including 0, but not including1).

Math.random();
Test and see‹/›

Note: Math.random()Always returns a floating-point random number between 0 and1between floating-point numbers.

JavaScript Random Integer

Math.random()with Math.floor()can be used together to return random integers.

This example returns a random integer from 0 to9to

Math.floor(Math.random() * 10);
Test and see‹/›

This example returns a random integer from 0 to10to

Math.floor(Math.random() * 11);
Test and see‹/›

This example returns a random integer from1to10to

Math.floor((Math.random() * 10) + 1);
Test and see‹/›

This example returns a random integer from1to100's random integer:

Math.floor((Math.random() * 100) + 1);
Test and see‹/›

This example returns a random integer from11to20's random integer:

Math.floor((Math.random() * 10) + 11);
Test and see‹/›

This example returns a random integer from51to100's random integer:

Math.floor((Math.random() * 50) + 51);
Test and see‹/›

Get a random number between two values

This example returns a random number between min (inclusive) and max (exclusive):

function getRandom(min, max) {
   return Math.floor(Math.random() * (max - min)) + min;
}
Test and see‹/›

This example returns a random number between min and max (both inclusive):

function getRandom(min, max) {
   return Math.floor(Math.random() * (max - min + 1)) + min;
}
Test and see‹/›

More Examples

Return the random number of mouse movement:

Move the mouse pointer over the following DIV:

Hover over me!!!
Run Code