Sabari M Sabari M
Updated date Aug 25, 2020
In this blog, we will learn how to generate a Random string and Random password using Random class and its methods.
  • 1.7k
  • 0
  • 2

In C#, we can easily generate the random string using the same concepts which are used to generate the Random numbers. The StringBuilder class can be used along with the NextDouble() method to generate the Random string.

If you are not aware of how to generate Random Numbers in C#, Please go through the below blog,

Generate Random Numbers In C#

Below sample shows how to generate the random string using C# in-built methods,

StringBuilder strbuilder = new StringBuilder();
Random random = new Random();

for (int i = 0; i < 8; i++)
{
	// Generate floating point numbers
	double myFloat = random.NextDouble();
	// Generate the char using below logic
	var myChar = Convert.ToChar(Convert.ToInt32(Math.Floor(25 * myFloat) + 65));                
	strbuilder.Append(myChar);
}

Console.WriteLine("Generate Random String : ");
Console.WriteLine(strbuilder.ToString());

// Output

// Generate Random String:
// SRWQKKKI

How to generate Random Password in C# :

Below code snippet shows to generate the random password,

using System;
using System.Text;

namespace ConsoleApp
{
    class RandomPassword
    {
        static void Main(string[] args)
        {
            RandomPasswordGenerator rpg = new RandomPasswordGenerator();
            Console.WriteLine("Generate Random Password: ");
            Console.WriteLine(rpg.GetPassword());
            Console.ReadKey();
        }
    }

    public class RandomPasswordGenerator
    {
        /// <summary>
        ///  Construct and return a random password 
        /// </summary>
        /// <returns>random password </returns>
        public string GetPassword()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(RandomString(3));
            builder.Append(RandomNumber());
            builder.Append(RandomString(4));            
            return builder.ToString();
        }

        /// <summary>
        /// Generate and return a random number 
        /// </summary>
        /// <returns>random number</returns>
        private int RandomNumber()
        {
            Random random = new Random();
            return random.Next(1000, 5000);
        }

        /// <summary>
        /// Generate and return a random string    
        /// </summary>
        /// <param name="length">length of the string</param>
        /// <returns>random string</returns>
        private string RandomString(int length)
        {
            StringBuilder strbuilder = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                // Generate floating point numbers
                double myFloat = random.NextDouble();
                // Generate the char
                var myChar = Convert.ToChar(Convert.ToInt32(Math.Floor(25 * myFloat) + 65));
                strbuilder.Append(myChar);
            }
            return strbuilder.ToString().ToLower();
        }      
    }
}

 

ABOUT THE AUTHOR

Sabari M
Sabari M
Software Professional, India

IT professional with 14+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!
×