In this article, we will learn how to generate a random alphanumeric string between a given range of a random length in C#. Random String is sometimes needed. So you can generate random alphanumeric string between a given range of a random length by using this code snippet. This function allows you to quickly generate random strings between a given range of a random length with c# and can be used for random identifiers, codes, semi-secure passwords and anywhere else where you may require a random string to be used.
Here is the code to Generate Alphanumeric Random String between a given range of a random length and in this example, it will generate an alphanumeric string from a range between 8 to 15 characters long consisting of Uppercase and lowercase letters and numbers.
using System;
using System.Linq;
namespace Tutorialsrack
{
class Program
{
/* How to Generate Random AlphaNumeric String between a given range of a Random Length in C# */
static void Main(string[] args)
{
Console.WriteLine("Random AlphaNumeric String is {0}", GenerateRandomAlphaNumericString(new Random().Next(8,15)));
Console.ReadKey();
}
//Method is used to Generate Alphanumeric String of a Specific Range
public static string GenerateRandomAlphaNumericString(int length)
{
Random random = new Random((int)DateTime.Now.Ticks);
//Characters used in for Generating AlphaNumeric String
string input = "abcdefghijklmnopqrstuvwxyzQAZWSXEDCRFVTGBYHNUJMIKLOP0123456789";
return new string(Enumerable.Range(0, length).Select(x => input[random.Next(0, input.Length)]).ToArray());
}
}
}
Random AlphaNumeric String is E14rBsaJh6a
I hope this article will help you to understand how to generate Random alphanumeric string between a given range of a random length in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments