static Random random = new Random(); List<string> generatedVouchers = new List<string>(); char[] keys = "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890".ToCharArray(); private static string GenerateVoucher(char[] keys, int lengthOfVoucher) { return Enumerable .Range(1, lengthOfVoucher) // for(i.. ) .Select(k => keys[random.Next(0, keys.Length - 1)]) // generate a new random char .Aggregate("", (e, c) => e + c); // join into a string } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { int vouchersToGenerate = 12; int lengthOfVoucher = 12; List<string> generatedVouchers = new List<string>(); char[] keys = "1234567890".ToCharArray(); //while (generatedVouchers.Count < vouchersToGenerate) //{ // var voucher = GenerateVoucher(keys, lengthOfVoucher); // if (!generatedVouchers.Contains(voucher)) // { // generatedVouchers.Add(voucher); // Response.Write(string.Format("<br>[#{0}] {1}", generatedVouchers.Count, voucher)); // } //} string vCode = GenerateVoucher(keys, lengthOfVoucher); Response.Write("<br>" + string.Format("{0}-{1}-{2}", vCode.Substring(0, 4), vCode.Substring(4, 4), vCode.Substring(8, 4))); //Response.Write("<br>" + string.Format("{0}", vCode.Substring(0, 4))); //Response.Write("<br>" + string.Format("{0}", vCode.Substring(4, 4))); //Response.Write("<br>" + string.Format("{0}", vCode.Substring(8, 4))); Response.Write("<br>-------------------------------------------------------------------<br>"); //********************************************************************************************** Random random = new Random(); DateTime timeValue = DateTime.MinValue; System.Text.Encoding big5 = System.Text.Encoding.GetEncoding("big5"); System.Text.Encoding gb2312 = System.Text.Encoding.GetEncoding("gb2312"); // Create 10 codes just to see the random generation. for (int i = 0; i < 10; ++i) { int rand = random.Next(3600); timeValue = timeValue.AddMinutes(rand); byte[] buffer = System.BitConverter.GetBytes(timeValue.Ticks); string voucherCode = ToBase32String(buffer); //BitConverter.ToString(buffer); Response.Write("<br>" + string.Format("{0}-{1}-{2}", voucherCode.Substring(0, 4), voucherCode.Substring(4, 4), voucherCode.Substring(8, 5))); } Response.Write("<br>-------------------------------------------------------------------<br>"); //********************************************************************************************** //System.Guid g = System.Guid.NewGuid(); //Random rn = new Random(); //string gs = g.ToString(); //int randomInt = rn.Next(5, 10 + 1); //Response.Write("<br>" + gs.Substring(gs.Length - randomInt - 1, randomInt)); } } public static string ToBase32String(byte[] input) { if (input == null || input.Length == 0) { throw new ArgumentNullException("input"); } int charCount = (int)Math.Ceiling(input.Length / 5d) * 8; char[] returnArray = new char[charCount]; byte nextChar = 0, bitsRemaining = 5; int arrayIndex = 0; foreach (byte b in input) { nextChar = (byte)(nextChar | (b >> (8 - bitsRemaining))); returnArray[arrayIndex++] = ValueToChar(nextChar); if (bitsRemaining < 4) { nextChar = (byte)((b >> (3 - bitsRemaining)) & 31); returnArray[arrayIndex++] = ValueToChar(nextChar); bitsRemaining += 5; } bitsRemaining -= 3; nextChar = (byte)((b << bitsRemaining) & 31); } //if we didn't end with a full char if (arrayIndex != charCount) { returnArray[arrayIndex++] = ValueToChar(nextChar); while (arrayIndex != charCount) returnArray[arrayIndex++] = '='; //padding } return new string(returnArray); } private static int CharToValue(char c) { int value = (int)c; //65-90 == uppercase letters if (value < 91 && value > 64) { return value - 65; } //50-55 == numbers 2-7 if (value < 56 && value > 49) { return value - 24; } //97-122 == lowercase letters if (value < 123 && value > 96) { return value - 97; } throw new ArgumentException("Character is not a Base32 character.", "c"); } private static char ValueToChar(byte b) { if (b < 26) { return (char)(b + 65); } if (b < 32) { return (char)(b + 24); } throw new ArgumentException("Byte is not a value Base32 value.", "b"); }