Skip to main content

C# method that generates a random string from a specified character set and length.

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(GetRandomString(20));
    }

    private static string GetRandomString(int length, string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
    {
        if (length < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(length), "length cannot be less than zero.");
        }

        if (string.IsNullOrEmpty(allowedChars))
        {
            throw new ArgumentException("allowedChars may not be empty.");
        }

        const int byteSize = 0x100;
        var allowedCharSet = new HashSet<char>(allowedChars).ToArray();
        if (byteSize < allowedCharSet.Length)
        {
            throw new ArgumentException($"allowedChars may contain no more than {byteSize} characters.");
        }

        using (var rng = RandomNumberGenerator.Create())
        {
            var result = new StringBuilder();
            var buf = new byte[128];

            while (result.Length < length)
            {
                rng.GetBytes(buf);
                for (var i = 0; i < buf.Length && result.Length < length; ++i)
                {
                    var outOfRangeStart = byteSize - (byteSize % allowedCharSet.Length);
                    if (outOfRangeStart <= buf[i])
                    {
                        continue;
                    }

                    result.Append(allowedCharSet[buf[i] % allowedCharSet.Length]);
                }
            }

            return result.ToString();
        }
    }
}


// Another example can be found here:
// https://github.com/OctopusDeploy/Sampler/blob/master/source/Sampler/Infrastructure/RandomStringGenerator.cs