In security, a random number is a value nobody can predict, used to make keys, passwords and codes.
This guide is for students, new developers and anyone curious why "random" matters so much in security. You do not need to be a mathematician: if an app you use sends password reset links, its safety depends on random numbers.
In short
- Keys, session codes and reset links are only safe if an attacker cannot guess them.
- Everyday random tools in programming languages are built for games and simulations, not for security.
- Security-grade generators start from real unpredictability that the computer collects.
- If randomness is weak, strong encryption can still be broken by guessing the key.
What you will learn
- Explain why security needs unpredictable numbers, not just numbers that look mixed up.
- Spot codes or links that follow a guessable pattern.
- Name the safe random tool to ask for in common programming languages.
- Ask a developer or supplier how their app makes its secret codes.
- Unpredictability (entropy)
- how hard a value is for an attacker to guess.
- Number maker (random number generator)
- software or hardware that produces random values.
- Starting value (seed)
- the secret value a number maker starts from.
What are random numbers in security?
Think of a pair of dice. Fair dice give results nobody can call in advance. Loaded dice look the same, but someone who knows the trick can predict the roll.
Security uses "dice" all the time. When an app makes an encryption key, a login session code or a password reset link, it needs a value nobody else can guess. Security people call this unpredictability entropy, and they measure it in bits.[1] More bits mean more possible values for an attacker to try.
Most computers cannot roll real dice quickly. So they use a random number generator. This is a recipe that starts from a secret seed (a starting value). From the seed, it makes a long stream of numbers that look random.[3] If the seed is guessable, the whole stream can be rebuilt.
Where the comparison stops working: loaded dice are often caught by watching enough rolls. Weak random numbers can pass a quick look and still be guessable by someone who knows how they were made.
Why does it matter?
Encryption is only as strong as its key. If the key comes from weak randomness, an attacker may skip the hard maths and just try the small number of likely keys.
This has happened. In 2008, Debian and Ubuntu warned that a change to their copy of a widely used encryption toolkit made its number maker predictable. Keys made on affected systems had to be treated as untrustworthy, even after the fix, and made again.[7][8]
If you are learning to code
What could happen: you use your language's everyday random tool to make login codes, and someone predicts them.
Check this: look up your language's security-grade random tool and use it for anything secret.
If you are studying security
What could happen: you learn that strong encryption is safe but miss that a weak key undoes it.
Check this: when you read about an encryption method, ask where its keys come from.
Next: Encryption for Beginners
If you buy software for a business
What could happen: a supplier's app sends reset links that count upwards, so a stranger can reach other customers' accounts.
Check this: ask suppliers how they create reset links and session codes.
What does it look like?
Here are made-up password reset links from two apps. Look at the codes at the end.
Example only. All values are made up.
# App A: reset links sent on Monday https://shop.example.com/reset?code=000123 (1) https://shop.example.com/reset?code=000124 (2) # App A: login session code SESSION-20261128-0915 (3) # App B: reset link sent on Monday https://print.example.com/reset?code=EXAMPLE-7QXf0000FAKEk2Lm0000 (4)
- In a real message, this line would contain the code that lets one customer reset their password.
- The next customer's code is one higher, so anyone who sees one code can guess the others.
- A session code built from the date and time, which an attacker can work out.
- A long code with no pattern, of the kind a security-grade generator makes.
Why this is risky: App A's codes follow patterns. A stranger who requests one reset link can guess the codes sent to other people.
The safer way: App B makes each code with a security-grade generator and makes it long enough that guessing should be impractical.[4]
How does it work?
Security-grade random numbers are made in stages.
- Collect. The computer gathers small signals nobody can predict, such as electrical noise in its parts. These are called entropy sources.[2]
- Mix. It mixes those signals into a secret seed.
- Stretch. A tested generator turns the seed into as many numbers as are needed. US government guidance sets out approved ways to do this.[3]
- Use. Programs ask the computer's own system for numbers. They use them for keys, codes and links.[5]

Most coding languages offer two kinds of tool. The everyday kind is fast and fine for games or shuffling a playlist. Its short name is PRNG. The security-grade kind has the short name CSPRNG. It is built so that seeing past numbers does not help you guess the next one.[6]
Take Python, a popular coding language. Its own help pages say its everyday random tool is made for modelling and games, not security. They point coders to a separate tool called "secrets" instead.[4] Web browsers offer a similar safe tool to web pages.[5] A free guide from OWASP, a security charity, lists the tool to avoid and the one to use in many languages.[6]

A worked example
Jana Okafor is a junior developer at Heron Printers, a fictional print shop that takes orders online. Her manager, Gideon Nakamura, asks her to add a "forgot your password" link to the website.
Jana writes the feature in an afternoon. To make each reset code she uses her language's everyday random tool, because it is the first one she finds.
Before the change goes live, Gideon reviews it. He asks one question: "Where does the code come from?" Jana explains. Gideon remembers the loaded dice. The codes look mixed up, but the everyday tool is not built to resist someone trying to predict it.
They check the language's documentation together. It names a separate security-grade tool for passwords and tokens. Jana swaps to it and makes each code long, following the documentation's suggested length.[4]
Gideon also asks for two more safeguards. Each reset code should work only once and expire after a short time. Both rules shrink the chance to guess. A stranger gets few tries, and only a short time to make them.
Finally, Jana adds a note to the team's coding guide. It says that anything secret must come from the security-grade random tool. Gideon adds a line to the shop's supplier questions, so future software is checked the same way.
Myths and mistakes
| What people think | What is true |
|---|---|
| If numbers look mixed up, they are random enough. | Looking random is not the same as being unpredictable. Everyday generators can be predicted by someone who knows how they work.[6] |
| Strong encryption cannot be broken, so randomness does not matter. | A guessable key undoes strong encryption. The 2008 Debian and Ubuntu problem is a well-known example.[7] |
| Using the current time as a starting value is fine. | Time is easy to guess. Security-grade tools start from unpredictable signals the system collects.[2] |
| Every random ID is safe to use as a secret. | Some ID formats include the time or device details. Check that the tool behind them is security-grade.[6] |
| I should write my own random generator to be safe. | Home-made generators are a common source of mistakes. Use the tested one your platform provides. |
What can I do this week?
- Look up the security-grade random tool for the language you use most.
- Search your own projects for the everyday random tool and check what each use is for.
- Request a password reset from a site you use and look at whether the code is long and patternless.
- Ask one software supplier how their app makes reset links and session codes.
Checklist
- Keys, tokens, reset codes and session codes come from a security-grade generator.
- Everyday random tools are used only for games, tests and simulations.
- Nobody uses the time, a counter or a user ID as a secret code.
- Secret codes are long enough that guessing should be impractical.
- Reset codes work once and expire after a short time.
- Keys made on a system later found to have weak randomness are replaced.
- Suppliers can explain how their software makes secret codes.
Common questions
Can a computer make truly random numbers?
Partly. Computers collect small signals nobody can predict from their parts, then use a tested generator to stretch them into many numbers. The result is designed to be unpredictable to an attacker, which is what security needs. You do not need to build this yourself.
What is the difference between a PRNG and a CSPRNG?
Both make random-looking numbers from a starting value. An everyday PRNG is fast but can be predicted by someone who studies its output. A CSPRNG is designed so that past outputs do not reveal future ones, so it is the one to use for anything secret.
How long should a secret code or token be?
Long enough that guessing should be impractical. Python's documentation suggested 32 random bytes as sufficient for typical uses as of 2015, and notes this may grow as computers get faster. For one-time codes sent by text, short codes rely on limits and expiry instead.
What should I do if my app used the wrong random tool?
Switch to the security-grade tool first. Then replace anything secret the old tool made, such as keys, tokens and active reset links. Changing the code does not fix secrets already created, as the 2008 Debian and Ubuntu case showed.
Does this matter for my passwords?
Yes, if you use a password generator. Password managers usually use a security-grade generator. Passwords you make up yourself tend to follow human patterns, which is why generated passwords are often stronger. See Password Managers for Beginners.
Where next?
Ready for the detail? Read the practitioner guide: Random Number Generation and Entropy in Security Guide. It goes further into how entropy is measured, how generators are tested and approved, and which failures to look for in code reviews.
Also useful:
- Encryption for Beginners
- Key Management for Beginners
- Session Tokens and Cookies for Beginners
- Public Key Cryptography for Beginners
Related NHI Mgmt Group resources: Cryptographic Protocol Failures and Misuse Guide · Password Hashing and Key Derivation Guide · Cryptographic Key Management Guide
Key terms
- Entropy
- How much uncertainty an attacker faces in guessing a value, usually counted in bits.
- Random number generation
- Making values that cannot be predicted, for use in keys, tokens and codes.
- Seed
- The secret starting value a generator uses to produce its numbers.
- CSPRNG
- A random number generator designed so its output cannot practically be predicted.
- PRNG
- An everyday random number generator, fine for games but not for secrets.
- Nonce
- A value used once, often random, that stops old messages being replayed.
- Brute force attack
- Trying many possible values until one works.
Sources and further reading
- Glossary: entropy. Computer Security Resource Center, National Institute of Standards and Technology (NIST). https://csrc.nist.gov/glossary/term/entropy, accessed 28 Nov 2026.
- SP 800-90B, Recommendation for the Entropy Sources Used for Random Bit Generation. NIST, January 2018. https://csrc.nist.gov/pubs/sp/800/90/b/final, accessed 28 Nov 2026.
- SP 800-90A Rev. 1, Recommendation for Random Number Generation Using Deterministic Random Bit Generators. NIST, June 2015. https://csrc.nist.gov/pubs/sp/800/90/a/r1/final, accessed 28 Nov 2026.
- secrets: Generate secure random numbers for managing secrets. Python documentation, Python Software Foundation. https://docs.python.org/3/library/secrets.html, accessed 28 Nov 2026.
- Crypto: getRandomValues() method. MDN Web Docs, Mozilla. https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues, accessed 28 Nov 2026.
- Cryptographic Storage Cheat Sheet: Secure Random Number Generation. OWASP Cheat Sheet Series, OWASP Foundation. https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html, accessed 28 Nov 2026.
- USN-612-1: OpenSSL vulnerability. Ubuntu Security Notices, Canonical, 13 May 2008. https://ubuntu.com/security/notices/USN-612-1, accessed 28 Nov 2026.
- CVE-2008-0166. CVE Program, MITRE, published 13 May 2008. https://www.cve.org/CVERecord?id=CVE-2008-0166, accessed 28 Nov 2026.