C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
GetLetter: This static method on the RandomLetter type provides a way to get the next random letter.
Info: The GetLetter method provides a way to get another random letter. It internally references the static field Random variable.
RandomAnd: It calls the Next method and stores the result of that onto the evaluation stack as an integer local variable.
Then: It uses type conversion to convert the value of "0-25" to the letters "a-z". The range of letters "a-z" is exactly "a" places past zero.
CharCastsC# program that random lowercase letter
using System;
static class RandomLetter
{
static Random _random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter.
// ... Between 'a' and 'z' inclusize.
int num = _random.Next(0, 26); // Zero to 25
char let = (char)('a' + num);
return let;
}
}
class Program
{
static void Main()
{
// Get random lowercase letters.
Console.WriteLine(RandomLetter.GetLetter());
Console.WriteLine(RandomLetter.GetLetter());
Console.WriteLine(RandomLetter.GetLetter());
Console.WriteLine(RandomLetter.GetLetter());
Console.WriteLine(RandomLetter.GetLetter());
}
}
Output
i
q
f
t
o
Note: The static modifier is used here to simplify the program layout and reduce instantiations.