Search Here!

Saturday, February 16, 2013

Generate Random Number

Generate Random Number for specified string length and character set.

/// Generates the alphanumeric random code.

/// This is the set of the characters from which random code needs to generate.
/// The length of the string needed to generate random code.
/// The alphanumeric random code.

        protected string GetRandomNumber(string characterSet, int length)
            Random random = new Random();
            //The below code will select the random characters from the set
            //and then the array of these characters are passed to string
            //constructor to makes an (alpha) numeric string depends on the user need.
            string getRandomCode = new string(Enumerable.Repeat(characterSet, length)
                    .Select(set => set[random.Next(set.Length)]).ToArray());
            return getRandomCode;

Character Set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+}{“:?><,./’;[]|\"

No comments:

Post a Comment