Generate Random Number And Random String In C#

Generate Random Number And Random String In C#


The Random class of .NET class library provides functionality to generate random numbers in C#. The Random class constructors have two overloaded forms. It takes either no value or it takes a seed value.


Random number generator in C#
The following code returns a random number between the min and the max range.
  • Generate a random number between two numbers.
  • public int RandomNumber(int min, int max)
  • {
  • Random random = new Random();
  • return random.Next(min, max);
  • }



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. int num = random.Next(); 
The following code returns a random number less than 1000. 
  1. int num = random.Next(1000);  
The following code returns a random number between the min and the max range.
  1. / Generate a random number between two numbers  
  2. public int RandomNumber(int min, int max)  
  3. {  
  4.     Random random = new Random();  
  5.     return random.Next(min, max);  
  6. }  
You can even combine the two methods - RandomNumber and RandomString to generate a combination of random string and numbers. 
  1. // Generate a random string with a given size  
  2. public string RandomString(int size, bool lowerCase)  
  3. {  
  4.     StringBuilder builder = new StringBuilder();  
  5.     Random random = new Random();  
  6.     char ch;  
  7.     for (int i = 0; i < size; i++)  
  8.     {  
  9.         ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));  
  10.         builder.Append(ch);  
  11.     }  
  12.     if (lowerCase)  
  13.         return builder.ToString().ToLower();  
  14.     return builder.ToString();  
  15. }  
The following code generates a password of length 10 with first 4 letters lowercase, next 4 letters numbers, and last 2 letters as uppercase.
  1. // Generate a random password  
  2. public string RandomPassword()  
  3. {  
  4.     StringBuilder builder = new StringBuilder();  
  5.     builder.Append(RandomString(4, true));  
  6.     builder.Append(RandomNumber(1000, 9999));  
  7.     builder.Append(RandomString(2, false));  
  8.     return builder.ToString();  
  9. }  

Here is the complete code written in .NET Core 2.0.
  1. using System;    
  2. using System.Text;    
  3.     
  4. class RandomNumberSample    
  5. {    
  6.     static void Main(string[] args)    
  7.     {   
  8.         RandomGenerator generator = new RandomGenerator();    
  9.         int rand = generator.RandomNumber(5, 100);    
  10.         Console.WriteLine($"Random number between 5 and 100 is {rand}");    
  11.     
  12.         string str = generator.RandomString(10, false);    
  13.         Console.WriteLine($"Random string of 10 chars is {str}");    
  14.     
  15.         string pass = generator.RandomPassword();    
  16.         Console.WriteLine($"Random string of 6 chars is {pass}");    
  17.     
  18.         Console.ReadKey();    
  19.     }    
  20. }    
  21.     
  22. public class RandomGenerator    
  23. {    
  24.     // Generate a random number between two numbers    
  25.     public int RandomNumber(int min, int max)    
  26.     {    
  27.         Random random = new Random();    
  28.         return random.Next(min, max);    
  29.     }    
  30.     
  31.     // Generate a random string with a given size    
  32.     public string RandomString(int size, bool lowerCase)    
  33.     {    
  34.         StringBuilder builder = new StringBuilder();    
  35.         Random random = new Random();    
  36.         char ch;    
  37.         for (int i = 0; i < size; i++)    
  38.         {    
  39.             ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));    
  40.             builder.Append(ch);    
  41.         }    
  42.         if (lowerCase)    
  43.             return builder.ToString().ToLower();    
  44.         return builder.ToString();    
  45.     }    
  46.     
  47.     // Generate a random password    
  48.     public string RandomPassword()    
  49.     {    
  50.         StringBuilder builder = new StringBuilder();    
  51.         builder.Append(RandomString(4, true));    
  52.         builder.Append(RandomNumber(1000, 9999));    
  53.         builder.Append(RandomString(2, false));    
  54.         return builder.ToString();    
  55.     }    
  56. }    


Comments

Popular posts from this blog

How To Migrate MVC 3 Application To MVC 5

Populate a drop-down in Vue.js and Asp.net Core from an ajax call

Building a CRUD Application with Ag-Grid