批量生成3~6位不重复随机字符串,可自定义生成的数字字符组合。
private void button1_Click(object sender, EventArgs e)
{
this.UseWaitCursor = true;
Stopwatch sw = new Stopwatch();
sw.Start();
int ilen=(int)numlen1.Value;
int iup = (int)Math.Pow(10, ilen-1);
int idown= (int)(Math.Pow(10, ilen)-1);
List<string> list = GetRandomNum(ilen, iup,idown,(int)numcount1.Value);
sw.Stop();
this.UseWaitCursor = false;
if (list == null)
{
return;
}
SetText(list, sw.ElapsedMilliseconds);//数据写入文本文件
}
//产生不会重复的随机数 private List<string> GetRandomNum(int length, int up, int down,int icount)
{
List<string> list = new List<string>();
int iFirst = 0;
Random ro = null;
while (true)
{
ro = new Random(length * unchecked((int)DateTime.Now.Ticks));
length = 1;
iFirst = ro.Next(up, down);
list.Add(iFirst.ToString());
if (list.Count == icount)
break;
}
return list;
}
评论