Generate Random Number And Random String In C#
C# Random class provides functionality to generate random numbers in C#. The Random class constructors have two overloaded forms. It takes either no value or it takes a seed value
C# Random class provides functionality to generate random numbers in C#. The Random class constructors have two overloaded forms. It takes either no value or it takes a seed value
public static string GetRandomString(int length,bool includeAlphabet =true, bool includeNumbers = true,
bool includespecialChar = false,bool capsOnly=false)
{
Random ran = new Random();
String b = "";
if (includeAlphabet)
b += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (includeNumbers)
b += "1234567890";
if(includespecialChar)
b+="!@#$%^&*()_";
String random = "";
for (int i = 0; i < length; i++)
{
int a = ran.Next(b.Length);
random = random + b.ElementAt(a);
}
return (capsOnly?random.ToUpper(): random);
}
Login for comment