← Back to Blog

Generating Secure Random Values: UUIDs, Tokens, and Passwords

April 4, 2026 3 min read By CodeTidy Team

The Dark Side of Randomness: How Insecure Random Values Can Compromise Your Application

We've all been there - needing a quick random value to generate a unique identifier or password. But have you ever stopped to think about the security implications of using Math.random() or other seemingly innocuous random number generators? The truth is, these methods can compromise the security of your application, and it's time to shine a light on the dark side of randomness.

Table of Contents

  • The Pitfalls of Math.random()
  • UUID Security: The Good, the Bad, and the Ugly
  • Generating Secure Random Values with crypto.getRandomValues()
  • Creating Unguessable Passwords with secrets in Python
  • Best Practices for Secure Random Value Generation
  • Key Takeaways
  • FAQ

The Pitfalls of Math.random()

Math.random() is a convenient way to generate a random number, but it's not suitable for generating secure random values. The problem lies in its algorithm, which is designed for statistical randomness, not security. This means that the values generated by Math.random() can be predictable and vulnerable to attacks.

// Don't do this!
const insecureRandomNumber = Math.random();

In contrast, secure random values are designed to be unpredictable and resistant to attacks. They are essential for generating unique identifiers, passwords, and other sensitive data.

UUID Security: The Good, the Bad, and the Ugly

UUIDs (Universally Unique Identifiers) are widely used to generate unique identifiers, but their security is often misunderstood. While UUIDs can be secure, their security depends on the method used to generate them.

// Good: Using crypto.randomUUID() to generate a secure UUID
const secureUUID = crypto.randomUUID();

crypto.randomUUID() is a secure way to generate a UUID, as it uses a cryptographically secure pseudo-random number generator (CSPRNG). However, not all UUID generation methods are created equal. Some methods, such as using Math.random() to generate a UUID, are insecure and should be avoided.

Generating Secure Random Values with crypto.getRandomValues()

crypto.getRandomValues() is a powerful method for generating secure random values. It uses a CSPRNG to generate an array of random numbers, which can be used to generate unique identifiers, passwords, and other sensitive data.

// Generate an array of 16 random numbers
const randomValues = new Uint8Array(16);
crypto.getRandomValues(randomValues);

Creating Unpredictable Passwords with secrets in Python

In Python, the secrets module provides a secure way to generate random values, including passwords. secrets uses a CSPRNG to generate unpredictable values, making it ideal for generating passwords and other sensitive data.

import secrets

# Generate a random password
password = secrets.token_urlsafe(16)

Best Practices for Secure Random Value Generation

When generating secure random values, it's essential to follow best practices to ensure the security of your application. Here are some key takeaways:

  • Use a CSPRNG to generate secure random values.
  • Avoid using Math.random() or other insecure random number generators.
  • Use a secure method to generate UUIDs, such as crypto.randomUUID().
  • Use a secure library, such as secrets in Python, to generate random values.

Key Takeaways

  • Secure random values are essential for generating unique identifiers, passwords, and other sensitive data.
  • Use a CSPRNG to generate secure random values.
  • Avoid using Math.random() or other insecure random number generators.

FAQ

Q: What is a CSPRNG?

A: A CSPRNG (Cryptographically Secure Pseudo-Random Number Generator) is a random number generator designed to generate unpredictable and secure random values.

Q: Is Math.random() secure?

A: No, Math.random() is not secure and should not be used to generate secure random values.

Q: Can I use crypto.getRandomValues() to generate a UUID?

A: Yes, you can use crypto.getRandomValues() to generate a UUID, but it's recommended to use crypto.randomUUID() instead, as it's a more convenient and secure way to generate a UUID.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp