开发者

Random numbers in C#

开发者 https://www.devze.com 2023-01-20 08:00 出处:网络
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 want to call a method c

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 want to call a method called randomnumber. Then I want the results to be displayed in lblPickFive_1. I have 2x2,Pick5,and powerball. Each random number is to be displayed in its own label I created.

For now I am just looking to get past the one of generating a random number and having it displayed in the one label then I will move onto the rest. And I am sure post more questions if I cant figure out the rest.

Error 1 No overload for method 'RandomNumber' takes '0' arguments

using System;
using System.Windows.Forms;

namespace LotteryTickets
{
public partial class Form1 : Form
{
    /// <summary>
    /// no-args Constructor
    /// </summary>
    public Form1()
    {
        InitializeComponent();

    }


    #region "== Control Event Handlers =="
    private void Form1_Load(object sender, EventArgs e)
    {
        ClearWinningNumbers();
    }


    #endregion "== End Control Event Handlers =="


    #region "== Methods ==";
    /// <summary>
    /// Clears the text inside the winning number "balls"
    /// </summary>
    private void ClearWinningNumbers()
    {
        this.lblPickFive_1.Text = "";
        this.lblPickFive_2.Text = "";
        this.lblPickFive_3.Text = "";
        this.lblPickFive_4.Text = "";
        this.lblPick开发者_运维问答Five_5.Text = "";

        this.lblTwoByTwo_1.Text = "";
        this.lblTwoByTwo_2.Text = "";

        this.lblPowerball_1.Text = "";
        this.lblPowerball_2.Text = "";
        this.lblPowerball_3.Text = "";
        this.lblPowerball_4.Text = "";
        this.lblPowerball_5.Text = "";

        this.lblPowerball_PB.Text = "";
    }
    #endregion "== End Methods ==";

    private void cblTwoByTwo_2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void cblTwoByTwo_1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void btnPlay_Click(object sender, EventArgs e)
    {
        RandomNumber();

    }

    private void lblPickFive_1_Click(object sender, EventArgs e)
    {

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


First, you shouldn't be newing up a random number generator every time you want a new random number. You should set the generator as a static or member variable and refer to it for each new number.

Second, you have to pass a min and a max to your RandomNumber method.


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.


You need to pass in values:

private void btnPlay_Click(object sender, EventArgs e)
{
    RandomNumber();

}

should be:

private void btnPlay_Click(object sender, EventArgs e)
{
    RandomNumber(0, 50000);

}


Your RandomNumber method takes two arguments.

If you want to call the method, you need to pass two arguments.


You're calling RandomNumber(); in btnPlay_Click but the RandomNumber method requires min and max.


Setup one Random object and initialize it once.

class Form1
{
    ...
    Random rnd = new Random();
}

then to use it every time it is needed

void RandomNumber(int min, int max)
{
    int num = rnd.Next(min, max);
    ...
}

What happens everytime you call new() it re-seeds the random number and you may end up with the same numbers over and over. I have had this happend to me and it killed me

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号