How Math.random() Works
Math.random() is one of the most commonly used methods in JavaScript when it comes to generating random values. It is easy to use and opens up possibilities for everything from small interactive features to more advanced games and simulations.

What is Math.random() and how does it work?
Math.random() is a built-in method in JavaScript that returns a floating-point number between 0 (inclusive) and 1 (exclusive). This means the value is always greater than or equal to 0, but never reaches exactly 1. This makes it perfect as a foundation for creating different types of randomness.
Under the hood, a pseudorandom number generator (PRNG) is used. This means the numbers are not truly random in a mathematical sense, but are calculated through algorithms. For most practical uses — such as games, lotteries, or randomized colors — Math.random() is good enough. However, for advanced cryptography or security purposes, it is not strong enough.
Examples of how Math.random() is used
A simple example is generating a random number between 0 and 9. This is done by multiplying the result from Math.random() by 10 and rounding down:
const number = Math.floor(Math.random() * 10)
In the same way, you can adjust the range by multiplying by different numbers and adding an offset. For example, if you want a number between 5 and 15, you can write:
const number = Math.floor(Math.random() * 11) + 5
Using the same principle, you can randomly generate everything from integers to decimals, pick random elements from an array, or create variation in colors, positions, and movements in a web application.
Simplify with a helper function
To make the code cleaner and easier to reuse, you can create a function in JavaScript that takes an array of values and returns one of them randomly.
const random = (array) => array[Math.floor(Math.random() * array.length)]
This function is used on Slumpgenerator to, among other things, generate a Yes or No, but it can just as well be used for any values you like.
const result = random(['Yes', 'No'])
result will at runtime be either Yes or No and can be presented to the user in whatever way is desired.
Common use cases for Math.random()
Math.random() is often used in small projects that need variation. A common example is generating random background colors or having a program pick a random quote from a list.
In game development, the method is used to generate things like starting positions, obstacles, loot, or random events. In simulations, it can be used to mimic uncertainty or natural variation. The method is also useful for creating test data when building an application and you want different values each time the program runs.
It is, however, important to remember that the randomness is not "true." Since the values are based on an algorithm, they can be predicted if the state of the generator is known. For games and simpler applications this does not matter, but for security-critical functions you should instead use methods based on cryptographic randomness.
Advantages and limitations of Math.random()
One of the great advantages of Math.random() is its simplicity — a single function call gives you a new random value. It is also standard in JavaScript and works in all modern browsers without any additional libraries.
One limitation, however, is that it is not suitable for situations where truly random numbers are required. For security-critical purposes, such as generating passwords or keys, the built-in Web Crypto API is recommended instead (e.g. crypto.getRandomValues()).
For the vast majority of use cases, Math.random() is more than sufficient. It gives developers a fast and flexible method for creating variation and unpredictability in their programs. Used correctly, it can contribute to everything from small fun features to complex simulations in JavaScript.
Read more about the method on Mozilla's website MDN.
Translations
- English: How Math.random() Works
- Svenska: Så fungerar Math.random()