JavaScript Random


Math.random()

The code Math.random() gives you a random number. It can be any number between 0 (including 0) and 1 (not including 1).

Math.random() gives you a number that's less than 1.


JavaScript Random Integers

The code Math.random() can generate random numbers between 0 (inclusive) and 1 (exclusive) when used alone. When combined with Math.floor(), it can be used to produce random whole numbers (integers).

There is no such thing as JavaScript integers.

We're discussing whole numbers, not ones with any decimal points


A Proper Random Function

From the examples shown earlier, it's a smart idea to make a reliable random function for any situation where you need random whole numbers.

This JavaScript function will give you a random number between a specified minimum value (inclusive) and a maximum value (exclusive):

This JavaScript function consistently provides a random number within the given range covers the smallest and largest values.