Random Generation of Various Types

Sorry for the lack of updates last week. I went on vacation to a spot where I was told there would be internet access... That was not the case. Anyway, I've already shown in the past how to do random string generation and even lorem ipsum generation. Today I'm building on that and showing easy ways to generate random values for various other data types including bool, enum, TimeSpan, and Color:

public class Random:System.Random
{
        /// <summary>
        /// Returns a random boolean value
        /// </summary>
        /// <returns>returns a boolean</returns>
        public bool NextBool()
        {
            if (Next(0, 2) == 1)
                return true;
            return false;
        }

        /// <summary>
        /// Gets a random enum value
        /// </summary>
        /// <typeparam name="T">The enum type</typeparam>
        /// <returns>A random value from an enum</returns>
        public T NextEnum<T>()
        {
            Array Values = Enum.GetValues(typeof(T));
            int Index = Next(0, Values.Length);
            return (T)Values.GetValue(Index);
        }

        /// <summary>
        /// Randomly generates a new time span
        /// </summary>
        /// <param name="Start">Start time span</param>
        /// <param name="End">End time span</param>
        /// <returns>A time span between the start and end</returns>
        public TimeSpan NextTimeSpan(TimeSpan Start, TimeSpan End)
        {
            if (Start > End)
            {
                throw new ArgumentException("The start value must be earlier than the end value");
            }
            return Start + new TimeSpan((long)(new TimeSpan(End.Ticks - Start.Ticks).Ticks * NextDouble()));
        }

        /// <summary>
        /// Returns a random color
        /// </summary>
        /// <returns>A random color between black and white</returns>
        public Color NextColor()
        {
            return NextColor(Color.Black, Color.White);
        }

        /// <summary>
        /// Returns a random color within a range
        /// </summary>
        /// <param name="MinColor">The inclusive minimum color (minimum for A, R, G, and B values)</param>
        /// <param name="MaxColor">The inclusive maximum color (max for A, R, G, and B values)</param>
        /// <returns>A random color between the min and max values</returns>
        public Color NextColor(Color MinColor, Color MaxColor)
        {
            return Color.FromArgb(Next(MinColor.A, MaxColor.A + 1),
                Next(MinColor.R, MaxColor.R + 1),
                Next(MinColor.G, MaxColor.G + 1),
                Next(MinColor.B, MaxColor.B + 1));
        }
}

They're all fairly simply and work well enough. The only one that may cause confusion would be the NextColor function. When you define the min/max value for the function, you're really defining the min/max for the A, R, G, and B values. So if you use black as the min and blue as the max, it will give you shades of blue only. If you use black and red, it gives you shades of red. Anyway, hopefully this will help you out a bit. So give it a try, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 8/18/2009 at 1:59 PM
Tags: ,
Categories: C#
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Validating a Domain Using Regex

This one is rather short, but basically I needed a regex string that could tell if what I was being fed was a web site or something else without actually going to said site. And really all I needed to check was that the data could possibly be a domain. That's it. So after looking at some of the more complex and most likely overkill examples out there, I decided to roll my own:

        /// <summary>
        /// Checks to see if a string is a valid domain
        /// </summary>
        /// <param name="Input">Input string</param>
        /// <returns>True if it is a valid domain, false otherwise</returns>
        public static bool IsValidDomain(string Input)
        {
            if (string.IsNullOrEmpty(Input))
                return false;
            Regex TempReg = new Regex(@"^(http|https|ftp)://([a-zA-Z0-9_-]*(?:\.[a-zA-Z0-9_-]*)+):?([0-9]+)?/?");
            return TempReg.IsMatch(Input);
        }

The code above simply looks for something starting with either http, https, or ftp and then  the domain, and finally the port if it exists. I could even use this to seperate out the various parts if need be (just sort out the various groupings). So it's simple, straightforward, and probably a bit buggy, but it's been working on my test data which is enough for now. So try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 8/4/2009 at 12:14 PM
Tags: , , ,
Categories: C#
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed