Random Character With LINQ


Just now, I checked my forgotten stackoverflow account. I looked on some common questions and upvoted the good ones. Then I stumbled to this great answer. Why it was so great? Because it solves common problem, just creating random characters, but using such advance thing. I mean people usually just use Random and create character. This awesome man, using LINQ, giving range of characters to create the random set of character. If you are too lazy to see that, you can see the code below.

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
var result = new string(
    Enumerable.Repeat(chars, 8)
              .Select(s => s[random.Next(s.Length)])
              .ToArray());

all credits goes to dtb

Author: Aditya Yedija Situmeang

Developer, Lover, Christian, Omnomnomnivore, Gamer.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s