Created
April 26, 2015 21:27
-
-
Save anonymous/35f658c3a292f3eaa172 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var list = new HashSet<string>(); | |
using (TextWriter writer = File.CreateText("codes.txt")) | |
{ | |
while(list.Count!= 100) | |
{ | |
var code = generateCode(8); | |
if (!list.Contains(code)) | |
{ | |
Console.WriteLine(code); | |
writer.WriteLine("BT-"+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