Get Random Color

88,221

Solution 1

Here's the answer I started posting before you deleted and then un-deleted your question:

public partial class Form1 : Form
{
    private Random rnd = new Random();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {  
        Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));

        BackColor = randomColor;
    }
}

Solution 2

The original version of your last method (pre-Edit) will return all different sorts of colors. I would be sure to use a single Random object rather than create a new one each time:

Random r = new Random();
private void button6_Click(object sender, EventArgs e)
{
    pictureBox1.BackColor = Color.FromArgb(r.Next(0, 256), 
         r.Next(0, 256), r.Next(0, 256));

    Console.WriteLine(pictureBox1.BackColor.ToString());
}

It produces all sorts of different colors:

Color [A=255, R=241, G=10, B=200]
Color [A=255, R=41, G=125, B=132]
Color [A=255, R=221, G=169, B=109]
Color [A=255, R=228, G=152, B=197]
Color [A=255, R=50, G=153, B=103]
Color [A=255, R=92, G=236, B=162]
Color [A=255, R=52, G=103, B=204]
Color [A=255, R=197, G=126, B=133]

Share:
88,221

Related videos on Youtube

cramopy
Author by

cramopy

#SOReadyToHelp I'm a 19 year old software developer/information technology officer. My favourite programming language is C# and I love it. :D public static int getRandomNumber() { return 4; //chosen by fair dice roll //guaranteed to be random } Also doing stuff with JavaScript, VBA and PHP all the time.

Updated on July 09, 2022

Comments

  • cramopy
    cramopy almost 2 years

    Do you know any method to generate a random Color (not a random Color Name!)?

    I've already got one, but this one is'nt doing it correctly:

    This only returns Green:

    Random r = new Random();
    BackColor = Color.FromArgb(r.Next(0, 256), r.Next(0, 256), 0);
    

    This only returns Red:

    Random r = new Random();
    BackColor = Color.FromArgb(r.Next(0, 256), 0, 0);
    

    This only returns Blue:

    Random r = new Random();
    BackColor = Color.FromArgb(0, 0, r.Next(0, 256));
    

    I want my Code to return one, random Color, not only green/red/blue every time, as the above ones do.

    How to solve this?

    Any suggestion will be approved with joy!

    • Dmitry
      Dmitry about 9 years
      Why do you think the last code returns only Blue?
    • fillobotto
      fillobotto about 9 years
      2) returns red because you are only setting the Red channel. 3) Returns blue because you are only setting the blue channel 1) Be careful that the two random you are picking probably equal each other since they are almost picked at the same time
    • Rufus L
      Rufus L about 9 years
      The code in the duplicate question (not the answer) is what you are looking for
  • Rufus L
    Rufus L about 9 years
    Exactly, the problem the OP was most likely having was creating a new random each time and then calling .Next, rather than re-using a global Random object.
  • rschoenbach
    rschoenbach over 3 years
    The above code did not work for me inside WPF project. Needed to cast the random values to a byte. Color.FromArgb((byte) rnd.Next(0, 256), (byte) rnd.Next(0, 256), (byte) rnd.Next(0, 256), (byte) rnd.Next(0, 256))