找传奇、传世资源到传世资源站!

C# 生成不重复随机字符串 示例

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

批量生成3~6位不重复随机字符串,可自定义生成的数字字符组合。
C# 生成不重复随机字符串 示例 C#语言基础-第1张

from clipboard
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;
        }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复