Other Posts in Randomization

  1. Random String/Password Generation in C#
  2. Lorem Ipsum Generator in C#
  3. Random Generation of Various Types

Random Generation of Various Types

8/18/2009

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:

   1: public class Random:System.Random
   2: {
   3:         /// <summary>
   4:         /// Returns a random boolean value
   5:         /// </summary>
   6:         /// <returns>returns a boolean</returns>
   7:         public bool NextBool()
   8:         {
   9:             if (Next(0, 2) == 1)
  10:                 return true;
  11:             return false;
  12:         }
  13:  
  14:         /// <summary>
  15:         /// Gets a random enum value
  16:         /// </summary>
  17:         /// <typeparam name="T">The enum type</typeparam>
  18:         /// <returns>A random value from an enum</returns>
  19:         public T NextEnum<T>()
  20:         {
  21:             Array Values = Enum.GetValues(typeof(T));
  22:             int Index = Next(0, Values.Length);
  23:             return (T)Values.GetValue(Index);
  24:         }
  25:  
  26:         /// <summary>
  27:         /// Randomly generates a new time span
  28:         /// </summary>
  29:         /// <param name="Start">Start time span</param>
  30:         /// <param name="End">End time span</param>
  31:         /// <returns>A time span between the start and end</returns>
  32:         public TimeSpan NextTimeSpan(TimeSpan Start, TimeSpan End)
  33:         {
  34:             if (Start > End)
  35:             {
  36:                 throw new ArgumentException("The start value must be earlier than the end value");
  37:             }
  38:             return Start + new TimeSpan((long)(new TimeSpan(End.Ticks - Start.Ticks).Ticks * NextDouble()));
  39:         }
  40:  
  41:         /// <summary>
  42:         /// Returns a random color
  43:         /// </summary>
  44:         /// <returns>A random color between black and white</returns>
  45:         public Color NextColor()
  46:         {
  47:             return NextColor(Color.Black, Color.White);
  48:         }
  49:  
  50:         /// <summary>
  51:         /// Returns a random color within a range
  52:         /// </summary>
  53:         /// <param name="MinColor">The inclusive minimum color (minimum for A, R, G, and B values)</param>
  54:         /// <param name="MaxColor">The inclusive maximum color (max for A, R, G, and B values)</param>
  55:         /// <returns>A random color between the min and max values</returns>
  56:         public Color NextColor(Color MinColor, Color MaxColor)
  57:         {
  58:             return Color.FromArgb(Next(MinColor.A, MaxColor.A + 1),
  59:                 Next(MinColor.R, MaxColor.R + 1),
  60:                 Next(MinColor.G, MaxColor.G + 1),
  61:                 Next(MinColor.B, MaxColor.B + 1));
  62:         }
  63: }

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.



Comments