Secure Random Number Generation for App Developers

By Sergey Nosov

May 31, 2025

Let us review generating random numbers securely in our applications, especially for cryptographic use cases like token or key generation. Randomness is deceptively tricky: if done wrong, it can break security in subtle ways. We'll first clarify pseudo-random versus true random generation, then look at specialized hardware sources (like Intel RDRAND or TPMs), followed by software-based generators (cryptographic PRNGs). We'll survey cloud services (Azure, AWS) that offer secure random bytes, and finally cover best practices in C# and JavaScript for tasks like generating tokens, password reset codes, or cryptographic keys.

Pseudo-Random vs True Random

Not all "random" numbers are created equal. A pseudorandom number generator (PRNG) is an algorithmic method that produces a sequence of numbers that only appear random. By definition, a PRNG's output is completely determined by its initial seed. In fact, a PRNG is "an algorithm for generating a sequence of numbers whose properties approximate…random numbers" but is not truly random, because it's entirely determined by that seed. For example, common programming libraries (like Python or Excel) use the Mersenne Twister algorithm, which is very fast and has good statistical properties, but if an attacker knows the seed they can predict all output. Cryptographic uses require unpredictability, so typical PRNGs must be cryptographically secure (CSPRNGs) to prevent any prediction.

By contrast, a true random number generator (TRNG) relies on physical, unpredictable events. For instance, it might measure electronic noise, radioactivity, or other quantum effects. WolfSSL describes that "for truly random numbers, the computer must use some external physical variable that is unpredictable, such as radioactive decay of isotopes or airwave static". These physical processes (like thermal or atmospheric noise) are inherently unpredictable at the quantum level, making TRNG output non-deterministic.

Key differences include:

In practice, cryptographers often seed a fast PRNG with entropy from a TRNG to get the best of both worlds. But for critical uses (keys, nonces), always assume you need unpredictability.

Hardware Random Number Generators

Modern hardware often includes dedicated true random number generators. These are typically silicon circuits that harvest noise. Some notable examples:

In summary, hardware RNGs (CPU instructions, TPMs, HSMs, etc.) provide high-quality entropy. They are generally fast and compliant with security standards. For instance, Intel's RNG (part of "Intel Secure Key") conditions raw noise through an AES-based PRNG to output 128-bit samples.

Software-Based (Cryptographic) Random Generators

If special hardware is unavailable, software libraries and OS features can still generate secure random numbers. The key is to use a cryptographically secure PRNG (CSPRNG) seeded from some entropy source. The CSPRNG must be unpredictable even if an attacker observes many outputs.

Common software approaches include:

In short, any software method must use an approved CSPRNG. As Wikipedia notes, a cryptographically secure pseudorandom number generator (CSPRNG) is simply "a pseudorandom number generator with properties that make it suitable for use in cryptography". In practice that means its output is unpredictable without knowing the internal state or seed. Common CSPRNG designs (e.g. those in NIST standards) are widely used in crypto libraries.

Randomness in Azure and AWS

Both Azure and AWS offer managed ways to get high-quality random data, often backed by HSMs:

In practice, if you already use Key Vault or KMS for keys, these services allow you to pull random data without managing seeds. This is useful for e.g. generating symmetric keys or nonces within the cloud environment.

Best Practices in C# and JavaScript

Finally, let's tie this to code. When writing C# or JavaScript, always use the built-in cryptographic RNG, not the general-purpose one:

C# (.NET)

JavaScript (Browser and Node.js)

Token/password/key generation tips: Use sufficient length of randomness. For instance, at least 128 bits (16 bytes) of entropy for tokens, and 256 bits for high-value keys. Encode binary data safely (e.g. Base64 or hex). Treat these values as secrets (don't log them). When generating things like password reset tokens, also consider time limits and one-use policies, but at the core the random bits must come from a CSPRNG as shown above.

To summarize:

By following these practices, your app will generate tokens, keys, and codes that are unpredictable to attackers.

References

WolfSSL, "True Random vs. Pseudorandom Number Generation," WolfSSL, July 13, 2021 (updated Oct. 14, 2024), https://www.wolfssl.com/true-random-vs-pseudorandom-number-generation/.

Wikipedia contributors, "Pseudorandom number generator," Wikipedia, last modified Feb. 22, 2025, https://en.wikipedia.org/​wiki/​Pseudorandom_number_​generator.

MDN Web Docs (Mozilla), "Math.random() – JavaScript," accessed May 30, 2025, https://developer.mozilla.org/en-US/docs/Web/​JavaScript/​Reference/​Global_Objects/Math/random.

MDN Web Docs (Mozilla), "Crypto.getRandomValues() – Web APIs," accessed May 30, 2025, https://developer.mozilla.org/​en-US/docs/​Web/API/​Crypto/getRandomValues.

Amazon Web Services, "GenerateRandom – AWS Key Management Service (KMS) API Reference," accessed May 30, 2025, https://docs.aws.​amazon.com/kms/​latest/APIReference/​API_GenerateRandom.html.

Microsoft, "Get Random Bytes – REST API (Azure Key Vault)," accessed May 30, 2025, https://learn.​microsoft.com​/rest/api/​keyvault/keys/​get-random-bytes.

Wikipedia contributors, "RDRAND," Wikipedia, last modified May 18, 2025, https://en.wikipedia.org/​wiki/RDRAND.

Suciu, Alin, and Tudor Carean. "Benchmarking the True Random Number Generator of TPM Chips." arXiv (July 2010), https://arxiv.org/​pdf/1008.2223.pdf.

GeeksforGeeks, "Node crypto.randomBytes() Method," last updated May 18, 2025, https://www.geeksforgeeks.org/​node-js-crypto-randombytes-method/.

GeeksforGeeks, "Node.js crypto.randomInt() Method," last updated June 8, 2022, https://www.geeksforgeeks.org​/node-js-crypto-​randomint-method/.

Wikipedia contributors, "Cryptographically secure pseudorandom number generator," Wikipedia, last modified Apr. 16, 2025, https://en.wikipedia.org/wiki/​Cryptographically_secure_​pseudorandom_number_generator.

Microsoft, "RNGCrypto​ServiceProvider Class (System.Security.Cryptography)," accessed May 30, 2025, https://learn.​microsoft.com/​dotnet/api/​system.security.​cryptography.​rngcryptoserviceprovider.

Microsoft, "RandomNumber​Generator.​GetInt32 Method (System.​Security.​Cryptography)," accessed May 30, 2025, https://learn.​microsoft.com/​dotnet/api/​system.security.​cryptography.​randomnumbergenerator.getint32.