Skip to content

Instantly share code, notes, and snippets.

@roboter
Forked from anonymous/genereteCodes.cs
Last active August 29, 2015 14:20
Show Gist options
  • Save roboter/d7b71b600dfa7ea1a1b8 to your computer and use it in GitHub Desktop.
Save roboter/d7b71b600dfa7ea1a1b8 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeGenerator
{
class Program
{
static void Main(string[] args)
{
var prefixes = new[] { "FD", "CL", "IN" };
foreach (var prefix in prefixes)
{
var list = new HashSet<string>();
using (TextWriter writer = File.CreateText(prefix + "_codes.txt"))
{
while (list.Count != 500)
{
var code = generateCode(8);
if (!list.Contains(code))
{
Console.WriteLine(code);
writer.WriteLine(prefix + "-" + code);
list.Add(code);
}
}
}
}
Console.ReadLine();
}
private static string _digits = "23456789";
private static string _lowers = "abcdefghjkmnpqrstuvwxyz";
private static string _uppers = "ABCDEFGHJKMNPQRSTUVWXYZ";
// Number of characters in _digits.
private static int _digitLen
{
get
{
return _digits.Length;
}
}
// Number of characters in _lowers.
private static int _lowersLen
{
get
{
return _lowers.Length;
}
}
// Number of characters in _uppers.
private static int _uppersLen
{
get
{
return _uppers.Length;
}
}
public static string generateCode(int pLength)
{
// unchecked truncates the value to fit int if it overflows (long -> int).
Random rnd = new Random(); //unchecked((int)DateTime.Now.Ticks)
string password = string.Empty;
for (int i = 0; i < pLength; ++i)
{
// Randomly select from which character set to take next character from.
// Letters are twice as likely compared to digits as there are more of them.
// Currently does not force all character sets to be included to the password.
switch (rnd.Next(5))
{
case 0:
{
password += _digits[rnd.Next(_digitLen)];
break;
}
case 1:
case 2:
{
password += _lowers[rnd.Next(_lowersLen)];
break;
}
case 3:
case 4:
{
password += _uppers[rnd.Next(_uppersLen)];
break;
}
}
}
return password;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment