static string randString()
{
string str = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+";//75个字符
Random r = new Random();
string result = string.Empty;
//生成一个8位长的随机字符,具体长度可以自己更改
while (result.Length<8)
{
int m = r.Next(0, 75);//这里下界是0,随机数可以取到,上界应该是75,因为随机数取不到上界,也就是最大74
string s = str.Substring(m, 1);
if (result.Contains(s)==false)
{ result += s; }
}
return result;
}
static void Main(string[] args)
{
Console.WriteLine(randString());
Console.Read();
}