Possible Duplicate:
How to get random string with spaces and mixed case?
How do I auto generate random alphanumercial characte开发者_开发问答rs
Valid characters: Uppercase (A-Z), Lowercase (a-z), and digits (0-9)
Size: 6
try this:
string def = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random rnd = new Random();
StringBuilder ret = new StringBuilder();
for (int i = 0; i < 6; i++)
ret.Append(def.Substring(rnd.Next(def.Length), 1));
return ret.ToString();
Something like this:
var possibleChars = new List<char>();
for(var c='a';c<='z';c++)
{
possibleChars.Add(c);
}
for (var c = 'A'; c <= 'Z'; c++)
{
possibleChars.Add(c);
}
for (var c = '0'; c <= '9'; c++)
{
possibleChars.Add(c);
}
var r = new Random();
var randomChar = possibleChars[r.Next(possibleChars.Count)];
精彩评论