如何通过单击按钮在C#中生成随机颜色?

时间:2022-10-28 18:20:36

I have a program which sends planes to different destination and it's implemented using threading. But the problem is my planes don't generate in random colors on button click, it generates random colors whenever the program is run. (I think this is because i haven't passed the method properly in the button clicking method)

我有一个程序将飞机发送到不同的目的地,并使用线程实现。但问题是我的飞机在按钮点击时不会以随机颜色生成,它会在程序运行时生成随机颜色。 (我认为这是因为我没有在按钮点击方法中正确传递方法)

Question is how can i change the code so that it generates random colors whenever the button is clicked?

问题是如何更改代码,以便在单击按钮时生成随机颜色?

And my code is given below. (There are few radio buttons which will prompt the user for the destination and when the button is clicked the planes will go)

我的代码如下。 (几个单选按钮会提示用户输入目的地,当点击按钮时,飞机将会运行)

my code for the button click instance

我的按钮单击实例的代码

    private void rbutton1_checked(Object sender, System.EventArgs e)
    {
        if (((RadioButton)sender).Checked == true)         
            destination = 1;
    }

    private void rbutton2_checked(Object sender, System.EventArgs e)
    {
        if (((RadioButton)sender).Checked == true)
            destination = 2;
    }

    private void rbutton3_checked(Object sender, System.EventArgs e)
    {
        if (((RadioButton)sender).Checked == true)
            destination = 3;
    }

    private void rbutton4_checked(Object sender, System.EventArgs e)
    {
        if (((RadioButton)sender).Checked == true)
            destination = 4;
    }

    private void btn_Click(object sender,
                           System.EventArgs e)
    {
        if (destination == 0) MessageBox.Show("Select an Option Please!!");

        else
        {
            for (int q = 0; q <1000; q++)
            {
                locked = !locked;
                this.btn.BackColor = locked ? Color.Pink : Color.LightGreen;
                lock (this)
                {
                    if (!locked)
                        Monitor.Pulse(this);
                }
            }
        }
    }

And my code for the random color generation which is not in the same class ( In the Form)

我的代码为随机颜色生成不属于同一类(在表单中)

public Color generateRandomColor()
    {

        Random random = new Random();
        Color color = Color.FromArgb(random.Next(255), random.Next(255), random.Next(255));
        return color;

    }

1 个解决方案

#1


Create on instance of Random class on load of your program. Then when user click on button to generate random color, it will give you different values. The problem is that you're always creating a new instance of the Random class, and this object starts with the same seed value by default, so you have always the same "random" number.

在程序加载时在Random类的实例上创建。然后当用户点击按钮生成随机颜色时,它会给你不同的值。问题是您始终在创建Random类的新实例,并且此对象默认以相同的种子值开头,因此您始终具有相同的“随机”数字。

But, pay attention that with code (in any language) you will get only a pseudo random. To get really random number you must have specific hardware to this.

但是,要注意使用代码(在任何语言中),您将只获得伪随机。要获得真正的随机数,您必须拥有特定的硬件。

#1


Create on instance of Random class on load of your program. Then when user click on button to generate random color, it will give you different values. The problem is that you're always creating a new instance of the Random class, and this object starts with the same seed value by default, so you have always the same "random" number.

在程序加载时在Random类的实例上创建。然后当用户点击按钮生成随机颜色时,它会给你不同的值。问题是您始终在创建Random类的新实例,并且此对象默认以相同的种子值开头,因此您始终具有相同的“随机”数字。

But, pay attention that with code (in any language) you will get only a pseudo random. To get really random number you must have specific hardware to this.

但是,要注意使用代码(在任何语言中),您将只获得伪随机。要获得真正的随机数,您必须拥有特定的硬件。