JS HTML DOMLesson #89

JavaScript Random

// Returns a random number:

Example

Example Code

javascript
Try it Yourself
// Returns a random number:

Math.random();

Share

JavaScript Math.random()

Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):

Math.random() always returns a number lower than 1.


JavaScript Random Integers

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

There is no such thing as JavaScript integers. > We are talking about numbers with no decimals here.

Example

Example Code

javascript
Try it Yourself
// Return a random integer from 0 to 9 (both included):

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

Explained:

Math.random() returns a floating-point number between 0 (inclusive) and 1 (exclusive).

Example outputs: 0.0, 0.237, 0.9999, but never 1.

Math.random() * 10 gives a range from 0 up to but not including 10.

Example possible results: 0.0, 3.5, 9.99, etc.

Math.floor() rounds a number down to the nearest whole integer: - 3.5 becomes 3 - 9.99 becomes 9 - 0.1 becomes 0 The possible integer results are then 0 through 9 (both inclusive). In other words, the range is [0, 9]. ### Example ``javascript // Return a random integer from 0 to 10 (both included): Math.floor(Math.random() * 11); ` ### Example `javascript // Return a random integer from 0 to 99 (both included): Math.floor(Math.random() * 100); ` ### Example `javascript // Return a random integer from 0 to 100 (both included): Math.floor(Math.random() * 101); ` ### Example `javascript // Return a random integer between 1 and 10 (both included): Math.floor(Math.random() * 10) + 1; `` Explained:

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

Multiplying by 10 gives a number from 0 up to but not including 10.

Adding 1 shifts that range to 1 up to but not including 11.

Math.floor() then rounds down, so you get an integer between 1 and 10.

Example

Example Code

javascript
Try it Yourself
// Returns a random integer from 1 to 100 (both included):

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

Summary

ExpressionRange fromRange to
Math.random()0

Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Returns a random number:

Math.random();

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Return a random integer from 0 to 9 (both included):

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

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Return a random integer from 0 to 10 (both included):

Math.floor(Math.random() * 11);

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Return a random integer from 0 to 99 (both included):

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

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Return a random integer from 0 to 100 (both included):

Math.floor(Math.random() * 101);

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Return a random integer between 1 and 10 (both included):

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