C# generate random string of different length
If you want to generate random strings of different different lengths, than use below function. Here size is an input variable for length of string.
public static string RandomString(int size)
{
string builder = string.Empty;
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder += ch.ToString();
}
return builder.ToString();
}
public static string RandomString(int size)
{
string builder = string.Empty;
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder += ch.ToString();
}
return builder.ToString();
}
Comments
Post a Comment