how to generate random string in 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: how to generate random numbers in c#

//works in visual studio for unity
int randomNumber = UnityEngine.Random.Range(1, 100);  //Random number between 1 and 99

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());
}