Random number generator
Ask someone to pick a random number between 1 and 10, and chances are they will say 7. Ask ten different people and you are even more likely to notice patterns. Humans are simply not good at making random choices. We like to think we are, but in reality, we tend to seek balance, avoid extremes, or fall back on familiar sequences. Even when we try not to, our brains are wired to detect and follow patterns, which is often a good thing. But when you genuinely need randomness, such as in a simulation, a lottery draw, or a test scenario, human intuition quickly falls short. The widget below does a slightly better job.
So, what about using a computer? Well, yes and no. Computers are notoriously bad at being truly random. Everything they do is based on logic and repetition. A typical so-called pseudo-random number generator, for example Math.random()
in JavaScript, may appear random at first glance, but it is actually an algorithm that produces a predictable sequence based on an initial value, called a seed. If you know the seed, you can accurately predict every subsequent number. That is useful for games or animations where repeatability matters, but not great if you need fairness or security.
That is why the widget below uses a safer method: window.crypto.getRandomValues()
. This is part of the Web Crypto API and gives access to a cryptographically secure random number generator. The results are far less predictable than with a pseudo-random number generator, which is exactly what you want when generating numbers for things like lotteries or secure applications.
But where does a computer actually get its randomness from? This is where entropy comes in, meaning a collection of unpredictable data gathered from the real world. Think of mouse movements, the timing of keystrokes, or subtle fluctuations in hardware such as temperature or voltage. These tiny, irregular signals feed a system that attempts to approximate true randomness. Some tech companies go even further. Cloudflare, for instance, has a wall of lava lamps that are constantly filmed. The unpredictable movement of light in the bubbling wax is used as a source of visual noise to help generate cryptographically secure keys. Not because it is required, but because it is possible, and because it works.
The tool above uses a similar kind of secure source. All you have to do is specify a minimum, a maximum, and how many numbers you want. The widget handles the rest. For example, set a minimum of 10, a maximum of 50, and ask for 5 numbers. You might get a list similar to 27, 14, 49, 22, 33. Each one is generated individually, evenly spread across the specified range, and cryptographically unpredictable. But because the process is completely random, it is also possible to get something like 11, 12, 13, 14, and 15. The chance is very small, but not zero.
The generated numbers are whole integers within the given range, including both endpoints. The algorithm uses a Uint32Array
filled via crypto.getRandomValues()
. Each number is then modulated to fit within the selected range. Since the generation is done independently for each number, duplicates can occur. If you want unique numbers, you will need an additional algorithm that filters out duplicates. This tool focuses on simplicity and speed.
Related tools
- UUID generator: Generate cryptographically secure UUID v4 identifiers with customizable formats for databases, APIs, and distributed systems.
Further Reading
- Wikipedia: Random number generation
Widget made with staark