In this example we’ll learn how to Generate Random Number And Random String in C#.
Random class constructors have two overloaded forms. It takes either no value or it takes a seed value. The Random class provides Random.Next(), Random.NextBytes(), and Random.NextDouble() methods. The Random.Next() method returns a random number, Random.NextBytes() returns an array of bytes filled with random numbers, and Random.NextDouble() returns a random number between 0.0 and 1.0. The Random.Next() method has three overloaded forms and allows you to set the minimum and maximum range of the random number. The following code returns a random number.
1 2 3 | int num = random.Next(); |
The following code returns a random number less than 100.
1 2 3 | int num = random.Next(100); |
The following code returns a random number between the min and the max range.
1 2 3 4 5 6 7 8 9 10 | // Instantiate random number generator. private readonly Random _random = new Random(); // Generates a random number within a range. public int RandomNumber(int min, int max) { return _random.Next(min, max); } |
You can even combine the two methods – RandomNumber and RandomString to generate a combination of random string and numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // Generates a random string with a given size. public string RandomString(int size, bool lowerCase = false) { var builder = new StringBuilder(size); // Unicode/ASCII Letters are divided into two blocks // (Letters 65–90 / 97–122): // The first group containing the uppercase letters and // the second group containing the lowercase. // char is a single Unicode character char offset = lowerCase ? 'a' : 'A'; const int lettersOffset = 26; // A...Z or a..z: length=26 for (var i = 0; i < size; i++) { var @char = (char)_random.Next(offset, offset + lettersOffset); builder.Append(@char); } return lowerCase ? builder.ToString().ToLower() : builder.ToString(); } |
Here is the complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | using System; using System.Text; namespace ConsoleApp7 { class RandomNumberSample { static void Main(string[] args) { var generator = new RandomGenerator(); var randomNumber = generator.RandomNumber(5, 100); Console.WriteLine($"Random number between 5 and 100 is {randomNumber}"); var randomString = generator.RandomString(10); Console.WriteLine($"Random string of 10 chars is {randomString}"); var randomPassword = generator.RandomPassword(); Console.WriteLine($"Random string of 6 chars is {randomPassword}"); Console.ReadKey(); } } public class RandomGenerator { // Instantiate random number generator. // It is better to keep a single Random instance // and keep using Next on the same instance. private readonly Random _random = new Random(); // Generates a random number within a range. public int RandomNumber(int min, int max) { return _random.Next(min, max); } // Generates a random string with a given size. public string RandomString(int size, bool lowerCase = false) { var builder = new StringBuilder(size); // Unicode/ASCII Letters are divided into two blocks // (Letters 65–90 / 97–122): // The first group containing the uppercase letters and // the second group containing the lowercase. // char is a single Unicode character char offset = lowerCase ? 'a' : 'A'; const int lettersOffset = 26; // A...Z or a..z: length = 26 for (var i = 0; i < size; i++) { var @char = (char)_random.Next(offset, offset + lettersOffset); builder.Append(@char); } return lowerCase ? builder.ToString().ToLower() : builder.ToString(); } // Generates a random password. // 4-LowerCase + 4-Digits + 2-UpperCase public string RandomPassword() { var passwordBuilder = new StringBuilder(); // 4-Letters lower case passwordBuilder.Append(RandomString(4, true)); // 4-Digits between 1000 and 9999 passwordBuilder.Append(RandomNumber(1000, 9999)); // 2-Letters upper case passwordBuilder.Append(RandomString(2)); return passwordBuilder.ToString(); } } } |
Output: