I have previously written article on how to Generate Registration Image on Fly
Enhancing the same password generation logic so that it would be useful for generating random password using C# code.
public static string GetRandomPassword(int length)
char chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
string password = string.Empty;
Random random = new Random();
for (int i = 0; i < length; i++)
int x = random.Next(1,chars.Length);
//Don't Allow Repetation of Characters
password += chars.GetValue(x);
Its a simple logic instead by generating a random number between 1 and Length of characters. It also checks that same character is not repeated in generated password and finally return the randomly generated password string of desired length.