Generating a random color in C#

Always wanted to have your own random color generator in C#? This snippet shows you how to do it.

public class RandomColorGenerator {

  Random r = new Random(DateTime.Now.Millisecond);

  public RandomColorGenerator(){
  }

  public Color RandomColor(){
    byte red = (byte)r.Next(0, 255);
    byte green = (byte)r.Next(0, 255);
    byte blue = (byte)r.Next(0, 255);

    return new Color(red, green, blue);
  }
}
This entry was posted in C# and tagged , , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Radicz
    Posted March 22, 2013 at 11:10 pm | Permalink

    class ColorRand
    {
    Random _r = null;

    public ColorRand()
    {
    _r = new Random();
    }
    public Color RandomColor()
    {
    return Color.FromArgb(_r.Next(0, 255), _r.Next(0, 255), _r.Next(0, 255));
    }
    }

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?