using System.Collections.Generic;
using System.Threading.Tasks;
static string alphaCaps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static string alphaLow = "abcdefghijklmnopqrstuvwxyz";
static string numerics = "1234567890";
static string special = "@#$-=/";
string allChars = alphaCaps + alphaLow + numerics + special;
public string GenerateRandomStrongPassword(int length)
String generatedPassword = "";
throw new Exception("Number of characters should be greater than 4.");
int lowerpass, upperpass, numpass, specialchar;
string posarray = "0123456789";
if (length < posarray.Length)
posarray = posarray.Substring(0, length);
lowerpass = getRandomPosition(ref posarray);
upperpass = getRandomPosition(ref posarray);
numpass = getRandomPosition(ref posarray);
specialchar = getRandomPosition(ref posarray);
for (int i = 0; i < length; i++)
generatedPassword += getRandomChar(alphaCaps);
generatedPassword += getRandomChar(alphaLow);
generatedPassword += getRandomChar(numerics);
else if (i == specialchar)
generatedPassword += getRandomChar(special);
generatedPassword += getRandomChar(allChars);
return generatedPassword;
private string getRandomChar(string fullString)
return fullString.ToCharArray()[(int)Math.Floor(r.NextDouble() * fullString.Length)].ToString();
private int getRandomPosition(ref string posArray)
string randomChar = posArray.ToCharArray()[(int)Math.Floor(r.NextDouble()* posArray.Length)].ToString();
pos = int.Parse(randomChar);
posArray = posArray.Replace(randomChar, "");
public static void Main()
Console.WriteLine("Hello World");
Program p = new Program();
string rs = p.GenerateRandomStrongPassword(8);