Random String Generation

2026-05-30·5 min read·Text

Use Cases

Random string generation is a common requirement in development, with use cases including: generating password reset tokens, creating unique identifiers, generating test data, creating temporary access codes, and generating verification codes. Different use cases have different randomness requirements - security-sensitive scenarios require cryptographically secure random numbers, while general scenarios can use regular pseudo-random numbers.

Understanding random number quality is crucial. JavaScript's Math.random() is convenient but not cryptographically secure and should not be used for passwords, tokens, or other security-sensitive scenarios. For security scenarios, use crypto.getRandomValues() or crypto.randomUUID() and other cryptographically secure random number generators.

Implementation Methods

// Simple random string (not secure)
function randomString(length) {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  for (let i = 0; i < length; i++) {
    result += chars.charAt(Math.floor(Math.random() * chars.length));
  }
  return result;
}

// Cryptographically secure random string
function secureRandomString(length) {
  const array = new Uint8Array(length);
  crypto.getRandomValues(array);
  return Array.from(array, byte =>
    byte.toString(16).padStart(2, '0')
  ).join('').slice(0, length);
}

// Generate random password (including uppercase, lowercase, numbers, special characters)
function generatePassword(length = 16) {
  const uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  const lowercase = 'abcdefghijklmnopqrstuvwxyz';
  const numbers = '0123456789';
  const special = '!@#$%^&*()_+-=';
  const allChars = uppercase + lowercase + numbers + special;

  let password = '';
  password += uppercase[Math.floor(Math.random() * uppercase.length)];
  password += lowercase[Math.floor(Math.random() * lowercase.length)];
  password += numbers[Math.floor(Math.random() * numbers.length)];
  password += special[Math.floor(Math.random() * special.length)];

  for (let i = password.length; i < length; i++) {
    password += allChars[Math.floor(Math.random() * allChars.length)];
  }

  return password.split('').sort(() => Math.random() - 0.5).join('');
}

Security Considerations

When generating secure random strings, cryptographic security must be considered. Regular random number generators are based on predictable algorithms - given the seed, the output can be predicted. Cryptographically secure random number generators (CSPRNGs) use system entropy sources to generate unpredictable random numbers. Token length is also important - session tokens should be at least 128 bits, and password reset tokens should be at least 64 bits.

Recommended Tools

Random.org is a true random number generator based on atmospheric noise. passwordsgenerator.net provides online password generation. Each language's crypto module provides cryptographically secure random number generation. Faker.js can generate various test data, including random strings.