Skip to content

Instantly share code, notes, and snippets.

@Ex094
Created November 20, 2013 15:51
Show Gist options
  • Save Ex094/7565457 to your computer and use it in GitHub Desktop.
Save Ex094/7565457 to your computer and use it in GitHub Desktop.
A small C# Snippet to generate random password of a given length
//Coded By Ex094
Random rnd = new Random(); //To Generate Random Numbers for string Index
string password = ""; //Variable to store the final Password
string passlist = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,./'[]~!@#$%^&*()_+=-|}{:?><";
Console.WriteLine ("Enter Password Length:");
string usrleng = Console.ReadLine(); //Get length from user in the form of string
int length = Convert.ToInt32(usrleng); //Convert the string to an int
for (int x = 0; x < length; x++) //Loop accroding to the length of the password
{
int pick = rnd.Next(1, passlist.Length); //Generate index number (Random)
password += passlist[pick]; //Get the random item from the passlist based on the random index number
}
Console.WriteLine("Your Generated Password Is: {0}", password); //Print the password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment