Le silly meaningless random string generator…

So here’s something I put together quickly by grabbing from stackoverflow, to generate some meaningless random string based on a given length of letters. 😀 This is actually a simple dot net implementation where as you could use it as a static class and use it anywhere you want. may be? up to you!

Here it is!

namespace WhateverYourNamespace
{
    public static class RandomWordGenerator
    {
        /// <summary>
        /// Just to generate some random words! 😛
        /// </summary>
        /// <param name="requestedLength"></param>
        /// <returns></returns>
        public static string GetMeaninglessRandomString(int requestedLength)
        {
            Random rnd = new Random();
            string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k",
                "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
            string[] vowels = { "a", "e", "i", "o", "u" };

            string word = "";

            if (requestedLength == 1)
            {
                word = GetRandomLetter(rnd, vowels);
            }
            else
            {
                for (int i = 0; i < requestedLength; i += 2)
                {
                    word += GetRandomLetter(rnd, consonants) + 
                        GetRandomLetter(rnd, vowels);
                }

                word = word.Replace("q", "qu").Substring(0, requestedLength);
                // We may generate a string longer than requested length, 
                // but it doesn't matter if cut off the excess.
            }

            return word;
        }

        private static string GetRandomLetter(Random rnd, string[] letters)
        {
            return letters[rnd.Next(0, letters.Length - 1)];
        }
    }
}

 

Now I don’t own this original implementation, I just put it into a static class and used in one of my apps. Sorry I don’t have the actual source of it. 😦 but I do recall I grab it from stackoverflow.

Anyways use it as you wish.

KTHANKSBYE!

lol

Advertisements

One thought on “Le silly meaningless random string generator…

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