using System.Collections.Generic;
using System.Security.Cryptography;
public const int seed = 203948209;
public static Random random = new Random(seed);
public static void Main()
for (var i = 0; i < 100; i++)
Console.WriteLine(RandomPassword.Generate(random));
public static class RandomPassword
public static string Generate(Random random = null)
int randomPasswordLength;
lock (RandomPassword._lock)
randomPasswordLength = (random ?? RandomPassword.RandomGenerator)
.Next(RandomPassword.MinimumPasswordLength, RandomPassword.MaximumPasswordLength + 1);
var identifier = new char[randomPasswordLength];
var randomData = new byte[randomPasswordLength];
using (var rng = new RNGCryptoServiceProvider())
rng.GetBytes(randomData);
for (var idx = 0; idx < identifier.Length; idx++)
var pos = randomData[idx] % RandomPassword.AvailableCharacters.Count;
identifier[idx] = RandomPassword.AvailableCharacters[pos];
return new string(identifier);
private const int MinimumPasswordLength = 18;
private const int MaximumPasswordLength = 24;
private static readonly object _lock = new object();
private static readonly Random RandomGenerator = new Random();
private static readonly IReadOnlyList<char> AvailableCharacters = new[]
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'