Random Number Generator
Generate random integers within any range, with optional uniqueness.
About the Random Number Generator
This Random Number Generator produces integer draws between two bounds. It supports both Math.random() — a fast non-cryptographic PRNG based on xorshift128+ in V8 — and crypto.getRandomValues(), which pulls entropy from the OS CSPRNG. For lottery picks, statistical sampling, and UI effects, Math.random is fine. For cryptographic use, security tokens, and audit-grade sampling, switch to the secure mode.
The tool implements two sampling modes: with replacement (the same number can appear more than once) and without replacement (each value appears at most once). The latter uses the Fisher–Yates shuffle — the unbiased algorithm described by Knuth in The Art of Computer Programming Vol 2 — which permutes an array of all possible values in O(n) time and takes the first count entries.
Common use cases include picking lottery numbers, drawing raffle winners, selecting randomized samples for A/B tests, and seeding test data for development environments. The output is reproducible across runs only if you capture the seed — this generator uses true entropy, so each click produces a fresh draw.
How It Works
The non-secure mode uses Math.floor(Math.random() * (max-min+1)) + min — the canonical JavaScript idiom for a uniformly distributed integer in [min, max]. Math.random in V8 is xorshift128+, which has a period of 2128-1 and passes BigCrush statistical tests. It is suitable for any non-security use case but should never be used for tokens, session IDs, or cryptographic nonces.
The secure mode replaces Math.random with crypto.getRandomValues(new Uint32Array(1))[0] % range. The modulo introduces a small bias when 232 is not evenly divisible by the range; for ranges under 1 million this bias is below 1 in 4,000 — negligible for any practical application. For ranges above 1 million, rejection sampling would eliminate bias entirely; we omit that for performance reasons.
For unique sampling (without replacement), the code builds an array [min, min+1, ..., max] and shuffles it with Fisher–Yates: for i from the end down to 1, swap element i with a random element from [0, i]. The first count entries of the shuffled array are the result. The shuffle is unbiased: every permutation is equally likely.
The mean of the output is reported as a quick statistical sanity check. For N draws from a uniform distribution on [min, max], the expected mean is (min+max)/2 with standard deviation (max-min)/sqrt(12*N). If your observed mean differs by more than 4σ, either the RNG is biased or you got astronomically unlucky.
Worked Examples
Draw 6 unique numbers from 1 to 49 (lottery-style). Typical output: 14, 23, 7, 38, 42, 19. Expected mean = 25.0; observed mean for this draw = 23.8, well within 1 standard deviation (~6.6 for N=6).
Draw 10 numbers with replacement from 1 to 100 in secure mode. Typical output: 73, 41, 92, 17, 88, 5, 56, 31, 64, 9. The CSPRNG output is statistically indistinguishable from Math.random for sampling purposes — the difference only matters if an attacker might predict the sequence.
Draw 5 unique numbers from 1 to 5 (the maximum possible unique draws). Output is always a permutation of 1, 2, 3, 4, 5 — there are exactly 120 possible permutations, each appearing with probability 1/120. Requesting 6 unique draws from 5 values would error out.
When to Use This Tool
- Lottery and raffle draws where fairness must be demonstrable.
- A/B test assignment — hashing the user ID into one of
Nbuckets for variant assignment. - Randomized sample selection for QA testing of a subset of records.
- Dice rolls, card draws, and other tabletop game mechanics for play-by-post or online play.
- Seeding procedural content generation in games (choose map layouts, item drops, NPC spawns).
- Picking lottery numbers — though note: random picks do not improve expected payout over a fixed sequence.
- Generating random test data for development databases or fixtures.
Limitations & Disclaimer
This tool generates integer draws using either Math.random (non-cryptographic) or crypto.getRandomValues (CSPRNG). The CSPRNG mode introduces a small modulo bias for ranges that do not evenly divide 232; for ranges under 1 million this bias is below 1 in 4,000 and negligible for sampling. For audit-grade unbiased sampling or cryptographic nonces, use rejection sampling or a dedicated library. Math.random must not be used for security-sensitive contexts. See our disclaimer for full terms.
Frequently Asked Questions
What is the difference between Math.random and crypto.getRandomValues?
<code>Math.random()</code> is a fast PRNG (xorshift128+ in V8) suitable for non-security use. Its state can be reconstructed from ~5 observed outputs. <code>crypto.getRandomValues()</code> pulls from the OS CSPRNG (<code>/dev/urandom</code>, <code>BCryptGenRandom</code>) and is suitable for security tokens, encryption nonces, and audit-grade sampling.
What is the Fisher-Yates shuffle?
Fisher–Yates (also called the Knuth shuffle) is the unbiased algorithm for generating a random permutation of an array in O(n) time. It walks from the end of the array to the front, swapping each element with a random element from the prefix. Every permutation is equally likely — there are no bias artifacts unlike naive ‘sort with random comparator’ approaches. See Knuth TAOCP Vol 2 §3.4.2.
Why does the tool cap the count at 1000?
Browser performance and DOM size. Generating 100,000 numbers takes milliseconds but rendering them as a comma-separated string slows the page. For large-scale random number generation, export the algorithm to a Node.js script or use a statistical package like NumPy.
Is the secure mode really cryptographically secure?
Yes for the entropy source, with a caveat about modulo bias. <code>crypto.getRandomValues(new Uint32Array(1))[0] % range</code> is biased by at most <code>range/2^32</code> per draw — for ranges under 1 million this is below 1 in 4,000, which is below the bias introduced by typical users picking numbers themselves. For audit-grade unbiased sampling, use rejection sampling.
Can I generate decimals or floats?
This tool generates integers only. For floats, multiply <code>Math.random()</code> by the range and add the minimum: <code>Math.random() * (max - min) + min</code> gives a uniform float in <code>[min, max)</code>. For Gaussian random variates, use the Box–Muller transform.
Will the same input produce the same output twice?
No. Each click draws fresh entropy. If you need reproducibility (e.g. for testing), seed a deterministic PRNG like <code>Math.seedrandom</code> or use <code>xoshiro128**</code> with a known seed. This tool is intentionally non-deterministic.
Last updated: September 9, 2026 · Author: HT99 Tools Editorial Team