Generate random alphanumeric strings in c#

We can generate a random alphabet by using the random class. Although Random class only returns an integer, we can use that to generate random alphabets.

Here we get 6 digits (RandomString(6); ) random alphanumeric string if you want to change you can pass any integer value.

 

Source code:-

 

    private static Random random = new Random();

 

    public static string RandomString(int length)

    {

        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        return new string(Enumerable.Repeat(chars, length)

            .Select(s => s[random.Next(s.Length)]).ToArray());

    }

 

// calling the RandomString method

 

protected void Page_Load(object sender, EventArgs e)

    {

        RandomString(6);

    }

Generate random alphanumeric strings in c#  Generate random alphanumeric strings in c# Reviewed by NEERAJ SRIVASTAVA on 7:36:00 PM Rating: 5

No comments:

Powered by Blogger.