create a password with a custom length and lowercase letters in csharp

main.cs
public static string GeneratePassword(int length)
{
    const string allowedChars = "abcdefghijklmnopqrstuvwxyz";
    var random = new Random();

    var password = new StringBuilder();

    for (int i = 0; i < length; i++)
    {
        int index = random.Next(allowedChars.Length);
        password.Append(allowedChars[index]);
    }

    return password.ToString();
}
371 chars
16 lines

This method takes an integer length as input and returns a randomly generated password of the specified length, composed solely of lowercase characters. It achieves this by using a StringBuilder to sequentially append randomly chosen lowercase alphabetic characters, with the character set defined in the allowedChars constant string.

gistlibby LogSnag