日期:2014-05-18  浏览次数:20914 次

求随即产生N个随即字符
public static 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'
  };

  public static string GetRandomStringByLength(int Length)
  {
  StringBuilder newRandom = new StringBuilder();

  Random rd = new Random(unchecked((int)DateTime.Now.Ticks));

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

  return newRandom.ToString();
  }


比如产生11个
  for (int i = 0; i < 11; i++)
  {
  GetRandomStringByLength(10)+"<br/>";
  }  

我这么做 都是重复的 为什么呢?
请高人指点!!

------解决方案--------------------
C# code
public static string GenerateRandom(int Length)
{
    StringBuilder builder = new StringBuilder(0x3e);
    Random random = new Random();
    for (int i = 0; i < Length; i++)
    {
        builder.Append(constant[random.Next(0x3e)]);
    }
    return builder.ToString();
}