开发者

Auto generate random alphanumerical characters? [duplicate]

开发者 https://www.devze.com 2023-04-06 21:15 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How to get random string with spaces and mixed case?
This question already has answers here: Closed 11 years ago.

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)];
0

精彩评论

暂无评论...
验证码 换一张
取 消