public static string MakePassword(string pwdchars, int pwdlen)
  {
      string tmpstr = "";
      int iRandNum;
      Random rnd = new Random();
     for (int i = 0; i < pwdlen; i++)
     {
       iRandNum = rnd.Next(pwdchars.Length);
       tmpstr += pwdchars[iRandNum];
     }
     return tmpstr;
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
       string randomchars = "0123456789";
       string[] password = new string[4];
        for (int i=0; i<4; i++)
        {
             passwords[i] = MakePassword(randomchars, 4);//为什么我这样写了一个for循环产生的密码都是一样的
        }  } 如何解决?

解决方案 »

  1.   

    static Random rnd = new Random();public static string MakePassword(string pwdchars, int pwdlen)
      

  2.   


    生成个密码。。没必要不重复啊。。各用各的密码帐号的
        /// <summary>
        /// 得到随机数
        /// </summary>
        /// <returns></returns>
        public string GetRandom()
        {
            string[] source = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            string code = "";
            Random rd = new Random();
            for (int i = 0; i < 6; i++)
            {
                code += source[rd.Next(0, source.Length)];
            }
            return code;
        }
      

  3.   

       public string GetRandom()
        {
            string[] source = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            string code = "";
            Random rd = new Random();
            for (int i = 0; i < 4; i++)
            {
                code += source[rd.Next(0, source.Length)];
            }
            return code;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < a; i++)
            {
                password[i] = GetRandom();
            }
         }为什么这样重复调用产生的密码还是全都一样呢!
      

  4.   

    public string GetRandom()
        {
            string[] source = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            string code = "";
            Random rd = new Random();
            for (int i = 0; i < 4; i++)
            {
                code += source[rd.Next(0, source.Length)];
            }
            return code;
        }    protected void Button1_Click(object sender, EventArgs e)
        {        
            SqlConnection SqlCon = new SqlConnection(ConfigurationManager.AppSettings["SqlConString"]);
            string str = "select className from login2";
            SqlDataAdapter myda = new SqlDataAdapter();
            SqlCon.Open();
            myda.SelectCommand= new SqlCommand(str,SqlCon);
            DataSet myds = new DataSet();
            myda.Fill(myds);
            int a = 0;
            a = myds.Tables[0].Rows.Count;
            string[] password = new string[4];
            for (int i = 0; i < a; i++)
            {
                password[i] = GetRandom();
                string str1 = "update login2 set password='"+password[i]+"'";
                SqlCommand Sql = new SqlCommand(str1,SqlCon);
                Sql.ExecuteNonQuery();
            }
            SqlCon.Close();
            Response.Write("<script language=javascript>alert('密码分配成功!')</script>");
        }
      

  5.   


    LZ。。细心点犯低级错误了
    "update login2 set password='"+password[i]+"'";你这句是个全表更新。。没有带条件。。你说类如果全部执行了。。你表中所有password都是最后生成的那个随机密
      

  6.   

    for循环里应该是i=0时  password='"+password[0]+"'  
    i=1时password='"+password[1]+"'  
      .......
      

  7.   

    GUID来代替 就不会重复了呵呵  这是最简单的GUID.NewGuid().ToString();
      

  8.   

    由于Random r = new Random() 的种子没有指定,生成的时间间隔太短导致生成结果一样
                Random rdm = new Random(Guid.NewGuid().GetHashCode());
                string RandomNumber = DateTime.Now.ToString("yyyyMMddhhmmssfff")  +"-" + rdm.Next(1000000, 9999999).ToString ();
                return RandomNumber;
      

  9.   

    我调用for循环来更新数据库应该不会是所有的密码都一样的呀?