What Math.random() Is
Math.random() is JavaScript’s built-in random number function. It returns a number from 0 up to (but not including) 1. The language specification doesn’t say which algorithm browsers must use, only that the result should be roughly uniform. Modern browsers use fast pseudo-random generators; the V8 engine in Chrome, for example, switched to an algorithm called xorshift128+ in 2015.
MDN, the standard reference for web developers, is explicit that Math.random() does not provide cryptographically secure random numbers and shouldn’t be used for anything security-related. For that, it points to crypto.getRandomValues().
Our Test: 100,000 Picks Each
We wanted to know whether the difference matters for picking random objects. So on 2026-11-03 we ran two pickers over our list of 1,207 objects, 100,000 picks each: one using Math.random() in the usual way, and one using the secure method our generator uses.
| Method | Least-picked object | Most-picked object | Chi-square | p-value |
|---|---|---|---|---|
Math.random() | 57 | 123 | 1157.8 | 0.837 |
crypto.getRandomValues() | 58 | 116 | 1202.8 | 0.521 |
| Expected for a fair picker | — | — | about 1,206 | above 0.0001 |
Both methods sit well below the threshold at which we would suspect bias (p = 0.0001). For picking objects, both are statistically fair.
The honest result: both passed. Each object came up about 82.85 times on average, and neither method showed any measurable preference for particular objects. For everyday use, Math.random() is random enough — though the random object generator still uses the secure method, for the reasons below.
How often the single most-picked object came up with each method, against the average for every object. Both sit within the range chance produces.
Where Bias Really Comes From
In practice, unfair random pickers are rarely caused by Math.random() itself. They are caused by how its output is turned into a choice. A common mistake is to take a random whole number and use the remainder after dividing by the number of items. With 16-bit numbers and our 1,207 objects, that makes 358 objects 1.85% more likely than the rest — a small but permanent bias that no amount of “better randomness” fixes.
Math.floor(Math.random() * n) avoids that problem for practical purposes, because Math.random() has vastly more possible values than there are objects, so any leftover imbalance is far too small to measure.
When Math.random() Is Fine
- Shuffling a playlist or a deck of cards in a casual game.
- Picking a random object, colour or prompt for fun.
- Animations, visual effects and simulations.
When to Use Something Stronger
- Passwords, tokens and anything security-related.
- Prize draws or giveaways where people need to trust the result.
- Anything where someone might try to predict the next value.
Why Our Generator Uses crypto.getRandomValues()
Given the test results, why bother? Three reasons. It costs nothing measurable in speed for a list this size. It removes any question of predictability, which matters when the object wheel is used for class draws or prizes. And it lets us say, without qualification, that every pick is as unpredictable as a browser can make it. You can see the result on the random object generator.
