Skip to content

Instantly share code, notes, and snippets.

@behnood-eghbali
Created April 11, 2021 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behnood-eghbali/f7560f0a0fed6f6b35b0f6c91eedf1b8 to your computer and use it in GitHub Desktop.
Save behnood-eghbali/f7560f0a0fed6f6b35b0f6c91eedf1b8 to your computer and use it in GitHub Desktop.
simple random password generator in c#
using System;
using System.Text;
public class PasswordGenerator
{
public static void Main()
{
int passwordLength = 15;
string password = GeneratePassword(passwordLength);
Console.WriteLine(password);
}
public static string GeneratePassword(int length)
{
const string characters = "!@#$%^&*()<>?-_=+;:,.0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var passwordBuilder = new StringBuilder(10, 20);
var randomNumber = new Random();
for (int i = 0; i < length; i++)
{
int index = randomNumber.Next(characters.Length);
passwordBuilder.Append(characters[index]);
}
return passwordBuilder.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment