Answers for "how to generate random letters in C#"

C#
14

how to generate random letters in C#

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());
}
Posted by: Guest on June-14-2020
2

random character c#

//Random Uppercase letter:
Random rnd = new Random();            
int ascii_index = rnd.Next(65, 91); //ASCII character codes 65-90
char myRandomUpperCase = Convert.ToChar(ascii_index); //produces any char A-Z

//random lowercase letter:
Random rnd2 = new Random();            
int ascii_index2 = rnd2.Next(97, 123); //ASCII character codes 97-123
char myRandomLowerCase = Convert.ToChar(ascii_index2); //produces any char a-z

//Random letter irrespective of case: (search up "random number from 2 different ranges" for explanation)
int i = rnd.Next(1, 3) == 1 ? rnd.Next(65, 91) : rnd.Next(97, 123);
char anyLetter = Convert.ToChar(i);
Posted by: Guest on May-06-2021

Code answers related to "how to generate random letters in C#"

C# Answers by Framework

Browse Popular Code Answers by Language