CSharp · Console
using System;
using System.Security.Cryptography;
public class Program
{
public static void Main(string[] args)
{
bool validLength = false;
int userDefinedLength = 0;
Console.WriteLine("How many characters would you like your password to be?");
do
{
try
{
userDefinedLength = int.Parse(Console.ReadLine());
validLength = true;
if (userDefinedLength < 3)
{
Console.WriteLine("Please enter something larger than 3.");
validLength = false;
}
}
catch (Exception)
{
Console.WriteLine("Please input a valid integer length.");
}
} while (validLength == false);
char[] passwordArray = new char[userDefinedLength];
int asciiValue = 0;
char asciiChar = ' ';
bool validPassword = false;
do
{
for (int i = 0; i < passwordArray.Length; i++)
{
int randomAsc = 0;
randomAsc = PseudoRandom.Next(1, 4);
//Console.WriteLine(randomAsc);
if (randomAsc == 1)
{
asciiValue = PseudoRandom.Next(65, 91);
//Console.WriteLine(asciiValue);
}
else if (randomAsc == 2)
{
asciiValue = PseudoRandom.Next(97, 123);
//Console.WriteLine(asciiValue);
}
else if (randomAsc == 3)
{
asciiValue = PseudoRandom.Next(48, 58);
//Console.WriteLine(asciiValue);
}
asciiChar = (char)asciiValue;
passwordArray[i] = asciiChar;
//validPassword = true;
}
bool isDigit = false;
bool isUpper = false;
bool isLower = false;
for (int i = 0; i < passwordArray.Length; i++)
{
if (char.IsDigit(passwordArray[i]))
{
isDigit = true;
}
if (char.IsUpper(passwordArray[i]))
{
isUpper = true;
}
if (char.IsLower(passwordArray[i]))
{
isLower = true;
}
}
if (isDigit == true && isUpper == true && isLower == true)
{
validPassword = true;
}
} while (validPassword == false);
Console.WriteLine("Your password is...");
foreach (char c in passwordArray)
{
Console.Write(c);
}
}
}
/// <summary>
/// A fast thread-safe wrapper for the default pseudo-random number generator.
/// </summary>
public static class PseudoRandom
{
// Global seed generator
private static RNGCryptoServiceProvider global;
// Thread-local pseudo-random generator
[ThreadStatic]
private static Random local;
// Gets or initializes a thread-local Random instance.
private static Random Local
{
get
{
Random random = local;
if (random == null)
{
byte[] buffer = new byte[4];
global.GetBytes(buffer);
int seed = BitConverter.ToInt32(buffer, 0);
random = local = new Random(seed);
}
return random;
}
}
static PseudoRandom()
{
global = new RNGCryptoServiceProvider();
}
/// <summary>
/// Returns a nonnegative random number.
/// </summary>
public static int Next()
{
return Local.Next();
}
/// <summary>
/// Returns a nonnegative random number less than the specified maximum.
/// </summary>
public static int Next(int maxValue)
{
return Local.Next(maxValue);
}
/// <summary>
/// Returns a random number within a specified range.
/// </summary>
public static int Next(int minValue, int maxValue)
{
return Local.Next(minValue, maxValue);
}
/// <summary>
/// Returns a random double between 0.0 and 1.0.
/// </summary>
public static double NextDouble()
{
return Local.NextDouble();
}
/// <summary>
/// Returns a nonnegative random double less than the specified maximum.
/// </summary>
public static double NextDouble(double maxValue)
{
return NextDouble(0.0, maxValue);
}
/// <summary>
/// REturns a random double within a specified range.
/// </summary>
public static double NextDouble(double minValue, double maxValue)
{
return Local.NextDouble() * (maxValue - minValue) + minValue;
}
}