Random 4 Digit Number Generator
Generate cryptographically secure random 4-digit numbers (0000-9999). Batch generate, filter duplicates, copy results instantly.
About
A 4-digit number spans the range 0000 to 9999, yielding exactly 10,000 possible values. Using Math.random for generation introduces bias due to its pseudo-random nature and reliance on predictable seed states. This tool uses the Web Crypto API (crypto.getRandomValues), a cryptographically secure pseudo-random number generator (CSPRNG) that draws entropy from the operating system. The distinction matters when generated numbers serve as PINs, verification codes, or sample identifiers where predictability constitutes a security or methodological flaw.
The generator preserves leading zeros. The value 0042 is a valid 4-digit string, not the integer 42. Filters for digit uniqueness reduce the pool from 10,000 to 4,536 permutations (10 × 9 × 8 × 7). Excluding specific digits further constrains the space. This tool reports the effective pool size so you can assess collision probability before generating a batch. Note: for sample sizes approaching the pool size, generation time increases due to rejection sampling for uniqueness.
Formulas
The generator produces a uniformly distributed 4-digit string from the range [0000, 9999]. The core operation uses rejection sampling on a cryptographic byte buffer:
To eliminate modulo bias, the raw 16-bit value (0 - 65535) is rejected if it falls in the biased tail: values ≥ 60000 are discarded and resampled. This ensures each of the 10,000 outcomes has equal probability p:
For unique-digit filtering, the available permutation count is:
where n = 10 − (number of excluded digits). Collision probability for k samples from a pool of size N follows the birthday approximation:
where k is the batch size and N is the effective pool size after applying all filters.
Reference Data
| Constraint | Pool Size | Formula | Example | Use Case |
|---|---|---|---|---|
| No constraint | 10,000 | 104 | 0000 - 9999 | General purpose |
| Unique digits only | 4,536 | P(10,4) − P(9,3) | 3847, 1052 | PIN codes, tokens |
| All unique, no zero | 3,024 | P(9,4) | 5193, 8274 | Numeric IDs |
| Only odd digits | 625 | 54 | 1357, 9911 | Odd-constrained sets |
| Only even digits | 625 | 54 (incl. 0) | 2460, 8802 | Even-constrained sets |
| Exclude 1 digit | 6,561 | 94 | No 7: 0235 | Avoid confusing chars |
| Exclude 2 digits | 4,096 | 84 | No 0,1: 2345 | Reduced ambiguity |
| Exclude 3 digits | 2,401 | 74 | No 0,1,2 | Specialized codes |
| Unique + exclude 1 | 3,024 | P(9,4) | 3859 | Secure tokens |
| Unique + exclude 2 | 1,680 | P(8,4) | 4567 | Lottery-style draws |
| Unique + exclude 3 | 840 | P(7,4) | 3456 | Small sample spaces |
| Starts with 1-9 | 9,000 | 9 × 103 | 1000 - 9999 | No leading zero |
| Palindromes only | 90 | 9 × 10 | 1221, 7337 | Novelty, memory aids |
| Repeated digits (1111-type) | 10 | 101 | 0000 - 9999 | Marker codes |
| Ascending digits | 210 | C(10,4) | 1234, 0589 | Sequential IDs |
| Sum of digits = 10 | 219 | Stars & bars with bounds | 1234, 0505 | Checksum-constrained |