How to create a random string of characters in C#?
- by Keltex
I'm trying to create random strings of characters. I'm wondering if there might be a more efficient way. Here's my algorithm:
string RANDOM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#@$^*()";
StringBuilder sb = new StringBuilder();
int length = rand.Next(10) + 1;
for (int idx = 0; idx < length; ++idx)
{
sb.Append(RANDOM[rand.Next(RANDOM.Length)]);
}
string RandomString = sb.ToString();
I'm wondering if the StringBuilder is the best choice. Also if selecting a random character from my RANDOM string is the best way.