Generating Random String in C#.
Posted by Viral Sarvaiya on May 14, 2012
.Net provide Random class to generate random number.
Here generating random string.
private readonly Random _rng = new Random();
private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
private string RandomString(int size)
{
char[] buffer = new char[size];
for (int i = 0; i < size; i++)
{
buffer[i] = _chars[_rng.Next(_chars.Length)];
}
return new string(buffer);
}
Thanks.
This entry was posted on May 14, 2012 at 12:13 AM and is filed under .Net, ASP.NET. Tagged: C#, Generating Random String in C#., Random, Random String. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.



Jeff Moden said
You might want to remove the vowels from all of that or you stand the chance of spelling some pretty offensive swear words.
Viral Sarvaiya said
Thanks for notifying me for that..