Skip to content

Generating a random alphanumeric code of 10 characters

September 5, 2013

I had searched to find a very simple way of randomly generating an alphanumeric code of 10 characters or more, and the easiest way is to use the System.Random class, it supports .NET Framework 2.0 and more. Here’s the code: –

        public static string GetRandomAlphaNumeric()
        {
            var chars = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”;
            Random random = new Random();
            return new string(chars.Select(c => chars[random.Next(chars.Length)]).Take(10).ToArray());
        }

The other ways i had found use LINQ and if you want to avoid using the overhead of LINQ, this is one of the best options.

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: