Random 4-Digit Number Generator
Why is this truly random?
This generator uses the Web Crypto API, the same cryptographic engine that secures HTTPS connections and encrypts your passwords. Unlike Math.random(), which relies on a predictable pseudorandom algorithm, crypto.getRandomValues() draws entropy from your operating system — hardware noise, interrupt timing, and other unpredictable physical sources.
The result is a number that is statistically uniform and cryptographically secure, meaning no one can predict the next value even if they know every previous one.
Use cases
- Generate a PIN code for a lock, safe, or device
- Create a one-time verification code
- Assign a random ticket or order number
- Pick a random year (1000–9999)
- Generate a random port number for development
- Create a numeric passcode for testing
How it works
- Your browser generates a cryptographically secure 32-bit unsigned integer
- The integer is mapped to the range 1000–9999 using modular arithmetic
- The result is always exactly 4 digits — no leading zeros, no truncation
Everything runs locally in your browser. No data is sent to any server, and no numbers are stored or logged.
Frequently asked questions
What range does this generate?
Numbers from 1000 to 9999 — all possible 4-digit numbers. That gives you 9,000 equally likely outcomes.
Is this safe to use as a PIN?
The generation itself is cryptographically secure. However, a 4-digit PIN has only 9,000 possibilities (or 10,000 if you include 0000–0999), which can be brute-forced quickly. Use longer codes for high-security applications.
Can I get numbers starting with zero?
No. This generator produces numbers from 1000 to 9999, so the first digit is always 1–9. If you need 4-digit codes with leading zeros (like 0042), use the password generator instead.
Is every number equally likely?
Yes. Each of the 9,000 possible 4-digit numbers has an equal probability of approximately 0.011% per generation.
Probability distribution
Each number from 1000 to 9999 has an exactly equal probability of being selected. The probability of any single outcome is:
The probability of generating a specific number twice in a row is approximately 1 in 81,000,000 — extremely unlikely, but not impossible with true randomness.
Common scenarios
PIN codes and passcodes
Phones, safes, bike locks, and door keypads commonly use 4-digit PINs. Generating one randomly is far more secure than choosing a memorable date or repeated digits, which are the first combinations an attacker will try.
Verification and OTP codes
Many services send 4-digit one-time passwords via SMS or email. If you're building a prototype or testing, this generator gives you realistic random codes instantly.
Ticket and reference numbers
Assign short, random reference numbers to orders, support tickets, or event entries. Four digits are easy to read aloud and type, while 9,000 possibilities are enough for small to medium batches.
Games and puzzles
Games like Mastermind and Bulls & Cows use secret 4-digit numbers. Generate a truly random target number that no player can predict.
PIN security: what to avoid
Research on leaked PIN databases shows that humans overwhelmingly choose predictable codes. The most common 4-digit PINs are:
Together, just 20 PINs account for over 25% of all choices. Using a random generator eliminates this human bias entirely — every number is equally likely, and none are "obvious."
Privacy and security
Your generated numbers never leave your device. This tool runs entirely in your browser using client-side JavaScript — no API calls, no server logs, no cookies, and no tracking of generated values. The source code is fully transparent and can be inspected in your browser's developer tools.
Sources of entropy
Entropy is the raw unpredictability that fuels random number generation. Different systems harvest entropy from different sources:
Hardware noise
Thermal noise in electronic circuits, clock jitter, and voltage fluctuations. This is what most operating systems use, including the entropy pool behind the Web Crypto API.
Atmospheric noise
Radio static from thunderstorms and electromagnetic interference. Services like RANDOM.ORG use this approach, capturing atmospheric noise via radio receivers.
Radioactive decay
The timing of individual atomic decay events is fundamentally unpredictable according to quantum mechanics. Some dedicated hardware RNGs use Geiger counters for this purpose.
User input
Mouse movements, keystroke timings, and touchscreen interactions contribute to the OS entropy pool. The precise microsecond timing of these events is unpredictable enough to be useful.