Random Numbers

Learn about random numbers using Math library.

The random() method

Every programming language provides a way of generating random numbers. These are always useful when coding, particularly when it comes to adding some element of chance to games.

Random…ish: It’s actually very difficult to produce a truly random number, so what programming languages produce are technically only pseudo–random numbers. Various factors are often used as a “seed” to generate the number—such as the time, or the position of the mouse pointer. Although these are deterministic (that is, not strictly random), when taken together they make it very difficult to replicate the same conditions, making the numbers effectively random.

JavaScript can generate random numbers using the Math.random() method. This will generate a number between 00 (inclusive) and 11 (exclusive).

Get hands-on with 1200+ tech skills courses.