Here I am giving a common function to generate a random string combination of Alphabates, Numbers and Special Charactors.
Call this function where ever you want with the size of the string that you want to generate. like this
public string GenerateString(int size)
{
Random rand = new Random();
string Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string Number = "0123456789";
string SpecialChar = "!@#$&";
char[] chars = new char[size];
int count = 0;
int j = Number[rand.Next(10)];
for (int i = j; i < (size*100); i++)
{
if (i % 2 == 0)
{
chars[count] = Alpha[rand.Next(26)];
count++;
if (count == size)
break;
}
if (i % 3 == 0)
{
chars[count] = Number[rand.Next(10)];
count++;
if (count == size)
break;
}
if (i % 5 == 0)
{
chars[count] = SpecialChar[rand.Next(5)];
count++;
if (count == size)
break;
}
}
string FinalCode = new string(chars);
return FinalCode;
}
{
Random rand = new Random();
string Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string Number = "0123456789";
string SpecialChar = "!@#$&";
char[] chars = new char[size];
int count = 0;
int j = Number[rand.Next(10)];
for (int i = j; i < (size*100); i++)
{
if (i % 2 == 0)
{
chars[count] = Alpha[rand.Next(26)];
count++;
if (count == size)
break;
}
if (i % 3 == 0)
{
chars[count] = Number[rand.Next(10)];
count++;
if (count == size)
break;
}
if (i % 5 == 0)
{
chars[count] = SpecialChar[rand.Next(5)];
count++;
if (count == size)
break;
}
}
string FinalCode = new string(chars);
return FinalCode;
}
Call this function where ever you want with the size of the string that you want to generate. like this
GenerateString(8);
No comments:
Post a Comment