Random numbers in C#

前端 未结 6 625
我寻月下人不归
我寻月下人不归 2021-01-24 19:52

I get the error below when I try to run the application I am sure its something simple but I dont see it. What I am trying to do it when I click a button I have labeled Play. I

6条回答
  •  失恋的感觉
    2021-01-24 20:42

    Well, your code doesn't match the error, but look at this:

    private void btnPlay_Click(object sender, EventArgs e)
    {
        RandomNumber();
    }
    
    private void RandomNumber(int min, int max)
    {
        int num = new Random().Next(min, max);
        lblPickFive_1.Text = num.ToString();
    }
    

    RandomNumber has two parameters, min and max. You haven't specified any in the call inside btnPlay_Click. That's what the compiler is complaining about. Change the call to something like:

    RandomNumber(5, 10);
    

    Even when that's fixed, you shouldn't create a new instance of Random each time. As it happens, it's unlikely to cause problems in this particular case as it's triggered by a user action, but you should read my article on random numbers to see what the problem is and how to avoid it.

提交回复
热议问题