random string generator c# code example

Example 1: c# random string

private static Random random = new Random();
public static string RandomString(int length)
{
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[random.Next(s.Length)]).ToArray());
}

Example 2: c# random generator

// One string will be randomly picked and displayed 
Random rnd = new Random();
string[] secOptions = {"string one", "string two", "string three"};
int randomNuber = rnd.Next(0, 3);
string secText = secOptions[randomNuber];
Console.WriteLine(secText);

Example 3: generate random alphanumeric string c#

Namespace: System.Web.Security
public string GeneratePassword()
{
	return Membership.GeneratePassword(10,1);
}

Example 4: c# random string

private static Random random = new Random();
public static string RandomString(int length)
{
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[random.Next(s.Length)]).ToArray());
}

Example 5: generate random string C#

private static Random random = new Random();

public static string RandomString(int length)
{
    const string chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[random.Next(s.Length)]).ToArray());
}

public static string[] RandomStrings_Type1(int strLength, int arraySize)
{
	const string chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";
  	string[] strArray = new string[arraySize];
    for (int i = 0; i < strArray.Length; i++)
    {
  		string randomString = new string(Enumerable.Repeat(chars, length)
                                     .Select(s => s[random.Next(s.Length)]).ToArray());
      	strArray[i] = randomString;
    }
  	return strArray;
}

public static List<string> RandomStrings_Type2(int strLength, int listSize)
{
	return RandomStrings_Type1(strLength, listSize).ToList();
}

Tags:

Php Example