Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Random Lowercase Letter

This C# article shows how to generate a random stream of lowercase letters.

Random lowercase letter. A random lowercase letter is needed.

It must be between "a" and "z" inclusive. We use the C# programming language. We generate random characters by providing inclusive and exclusive bounds to the Random variable.

Example. This program introduces two classes to the compilation unit. The RandomLetter class is a static class, meaning it cannot be instantiated. The Program class contains the Main entry point.

Note: The GetLetter static method on the RandomLetter type provides a way to get the next random letter.

C# program that generates 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.



The GetLetter method provides a way to get another random letter. It internally references the static field Random variable. 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".

Note: This is possible because the range of letters "a-z" is exactly "a" places past zero.

Random fields. When using the Random variable type, it is often useful to store the Random variable itself as a static field. This is because, logically, Random implements a stream of randomness, not true randomness.

Random Number

Summary. We examined a program text that implements a random letter generation routine. The method shown returns a random letter between "a" and "z" in char representation. It can be called sequentially as it is implemented as a random stream.

Note: The static modifier is used here to simplify the program layout and reduce instantiations.

Static Method, Class and Constructor

Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf