Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘Generating Random String in C#.’

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.

Advertisements

Posted in .Net, ASP.NET | Tagged: , , , | 2 Comments »

 
%d bloggers like this: