AlesRuzicka.eu
#csharp

One line password generation in .Net using LINQ

When I wrote this code in use-once application, I thought that is rather weird part of code, but works just fine for this once case.
Then I realized, there is nothing wrong with the code. People, who are familiar with LINQ will have little problem reading it and it will work 100% reliably.

Really in just 1 line you can generate (pseudo)random password from set of predefined characters. Advantage of predefined characters is, that you can remove characters that can cause problems. Because I live in Europe, I removed Z and Y as it may be in different places on keyboard base on keyboard layout. But you can also consider removing 0O or 1l combinations.

static Random random = new Random();
static string GeneratePassword(int lenght, string allowedChars) {
    return new string(Enumerable.Range(0,lenght).Select (a => allowedChars[random.Next(allowedChars.Length)]).ToArray());
}