想用c# 生成一组随机密码,可出现这样的情况:
  一组50个随机密码里,前10个一样,然后后12个一样,然后又9个一样的
  如果在生成密码的前边加个中断,按F10运行下去,结果就是一组中没有一个重复的...原码如下,求高人帮忙:        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "";
            string RomPass = "";
            string a1 = "";
            for (int i = 0; i < 10; i++)
            {
                RomPass = GetCharFont(18);
                a1 += RomPass + "\r";
            }
            label1.Text = a1;
        }        //得到数字加英文的随机数,参数:随机数长度,返回值:一个字符串!"
        public string GetCharFont(int strLength)
        {
            char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < strLength; i++)
            {
                newRandom.Append(constant[rd.Next(62)]);
            }
            return newRandom.ToString();
        }点击按钮执行结果:
 MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ在 button1_Click 加个中断,按F10运行:
qGVW4bY1JJ4DKZjlsl
TFOIctYyykmQKQnD9c
lHDgreawi2CIC6D4EY
OGv2zxb38DTUCYHmlQ
uK4qHV57NG1jWYt14V
YJWcQe6EChjvWPxjLM
a6cNqusepHaHWl0BUt
2hpNyYfRUa8iztsCEM
MrwqmjE1wUCaSrPgyC
m06gWKMIZaaLvdGgLK
ebkh4ezluD8m8k9hw3
nxL8RKLBLfiD4yUDtk
EY4TRCG6mtBdfCGqip
xd1pSiOuhPeoNq2u23
8MCfsKWbK6MZqbTufb
GWdrZi3QIv9PoFltqG
j4HCd98mhDfTz3ifXG
EwN61LdboFfnOpLYXb

解决方案 »

  1.   


    //得到数字加英文的随机数,参数:随机数长度,返回值:一个字符串!" 
            public string GetCharFont(int strLength) 
            { 
                char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; 
                System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62); 
                DateTime now = DateTime.Now;            //需要生成不同的随机数生成器
                Random rd = new Random(now.MillSecond * now.MillSecond); 

                
                for (int i = 0; i < strLength; i++) 
                { 
                    newRandom.Append(constant[rd.Next(62)]); 
                } 
                return newRandom.ToString(); 
            } 
      

  2.   

    随记的相同的概率是有的,你还不如结果hash一下
      

  3.   

    for (int i = 0; i < strLength; i++) 
                { 
                    newRandom.Append(constant[rd.Next(62)]); 
                    Thread.Sleep(10);//
                } 
      

  4.   


        protected void Page_Load(object sender, EventArgs e)
        {
            string a1 = "";
            for (int i = 0; i < 10; i++)
            {
                string RomPass = GetCharFont(i, 18);
                a1 += RomPass + "<br/>";
            }
            Response.Write(a1);
        }    //得到数字加英文的随机数,参数:随机数长度,返回值:一个字符串!" 
        public string GetCharFont(int randKey, int strLength)
        {
            char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
            Random rd = new Random(randKey);
            for (int i = 0; i < strLength; i++)
            {
                newRandom.Append(constant[rd.Next(62)]);
            }
            return newRandom.ToString();
        } 我改了一下,不过我的平台是webForm所以,有些语句改动了一下。着重点是 Random rd = new Random(randKey);
      

  5.   

    呃..楼上的
    Random rd = new Random(randKey);
    改成
    Random rd = new Random(Convert.ToInt32((DateTime.Now.Ticks + randKey) % Int32.MaxValue));
    比较正确。
      

  6.   

    1楼大哥的解决思路是对的,可是结果还不是很满意,依然大量出现重复码,但将now.MilliSecond 码对应出现发现,重复的随机码与now.MilliSecond码是一一对应的!!
    谢谢!!
      

  7.   

    谢谢大哥,问题解决了!!解决思路: 在Random时,系统默认好像是以时间为基础产生随机数值,那当调用Random过快时,每次的时间值一至的时候,产生的结果将是一至的!
    那这个时候将Random的基础改为一个一定变化的量,才能使结果产生变化。
    当然我们在重复、短时间内调用Random的话,一定会用到For 那么将For的循环值继承到Random基础值中,那么产生的结果就一定不同!
    Random rd = new Random(Convert.ToInt32((DateTime.Now.Ticks + Rom) % Int32.MaxValue));