JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Random Numbers

The Math.random() method returns a floating-point random number in the range 0-1 (inclusive of 0, but not 1).

Run code

Note: Math.random() always returns a floating-point number between 0 and 1.


JavaScript Random Integers

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

This example returns a random integer number from 0 to 9:

Run code

This example returns a random integer number from 0 to 10:

Run code

This example returns a random integer number from 1 to 10:

Run code

This example returns a random integer number from 1 to 100:

Run code

This example returns a random integer number from 11 to 20:

Run code

This example returns a random integer number from 51 to 100:

Run code

Getting a random number between two values

This example returns a random number between min (included) and max (excluded):

Run code

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

Run code

More Examples

Return a random number on mouse move:

Move your mouse pointer over the DIV below:

Mouse Over me!!!
Run code