要求字符串的格式是:AXXXX-XXXXX-XXXXX-XXXXX
其中X是A~Z 或者0~9中的隨機字符。
下麵的兩個方法GetRandomStr和MakeChar都可以。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
namespace generator
{
class Program
{
static void Main()
{
MessageBox.Show(MakeChar());
//MessageBox.Show(GetRandomStr(5));
}
static string GetChar()
{
Random chars = new Random((int)DateTime.Now.Ticks);
int RandomNum = chars.Next(0,9);
int RandomChar = chars.Next(65,90);
if (RandomNum % 4 == 0)
return RandomNum.ToString();
else
return ((char)RandomChar).ToString();
}
static string MakeChar()
{
string seed = "A";
for (int i = 1; i < 30; i++)
{
Thread.Sleep(i);
if ((i + 1) % 6 != 0)
seed += GetChar();
else
seed += "-";
}
return seed.Substring(0,29);
}
static string GetRandomStr(int seed)
{
const string charSeed = "ABCD" +
EFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random rand = new Random(seed + DateTime.Now.Millisecond);
string myChar = "A";
for (int i = 0; i < 24; i++)
{
myChar += charSeed.Substring(rand.Next(0, charSeed.Length -1), 1);
if ((i - 3) % 5 == 0 & i != 23)
myChar += "-";
}
return myChar;
}
}
}
在城裏 2012.05.14