开发者

Same number in all 5 boxes and it's supposed to be 5 different numbers in 5 different boxes [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicates:

Random numbers in C#

Random number generator not working the way I had planned (C#)

I get the same number in the 5 boxes. How can it be avoided?

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 开发者_JAVA技巧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.lblPickFive_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(1,20);
       }

       private void lblPickFive_1_Click(object sender, EventArgs e)
       {

       }

       public void RandomNumber(int min, int max)
       {
           int num = new Random().Next(min, max);
           lblPickFive_1.Text = num.ToString();
           int num2 = new Random().Next(min, max);
           lblPickFive_2.Text = num2.ToString();
           int num3 = new Random().Next(min, max);
           lblPickFive_3.Text = num3.ToString();
           int num4 = new Random().Next(min, max);
           lblPickFive_4.Text = num4.ToString();
           int num5 = new Random().Next(min, max);
           lblPickFive_5.Text = num5.ToString();
       }

       private void lblPickFive_2_Click(object sender, EventArgs e)
       {

       }
    }
}


Random() is seeded by the time it is created, because your system is fast enough all of the items get the same seed. use only one random object

public void RandomNumber(int min, int max)
{
    var rand = new Random()
    int num = rand.Next(min, max);
    lblPickFive_1.Text = num.ToString();
    int num2 = rand.Next(min, max);
    lblPickFive_2.Text = num2.ToString();
    int num3 = rand.Next(min, max);
    lblPickFive_3.Text = num3.ToString();
    int num4 = rand.Next(min, max);
    lblPickFive_4.Text = num4.ToString();
    int num5 = rand.Next(min, max);
    lblPickFive_5.Text = num5.ToString();
}

However two successive calls to RandomNumber(int min, int max) will give the same five results if they are called within the same time slice. the best solution (if you are not multithreaded) is make rand a static variable.


Here is a thread safe version, if you are not using rand from multiple threads you can drop the lock;

static Random rand = new Random();
public void RandomNumber(int min, int max)
{
    lock(rand)
    {
        int num = rand.Next(min, max);
        lblPickFive_1.Text = num.ToString();
        int num2 = rand.Next(min, max);
        lblPickFive_2.Text = num2.ToString();
        int num3 = rand.Next(min, max);
        lblPickFive_3.Text = num3.ToString();
        int num4 = rand.Next(min, max);
        lblPickFive_4.Text = num4.ToString();
        int num5 = rand.Next(min, max);
        lblPickFive_5.Text = num5.ToString();
    }
}


Think about this a little. You don't want random numbers, you want a random selection of N numbers from a set of M numbers.

//Powerball white balls (I think)
var randomInts = Enumerable.Range(1, 54)                   // 1-54
                           .OrderBy(x => Guid.NewGuid())   //random order
                           .Take(5)                        //5 for powerball 
                           .OrderBy(i => i);               //sort

If you model your solution after how it actually works, it's really pretty easy.


Try this:

    public void RandomNumber(int min, int max)
    {
        Random r = new Random(DateTime.Now.Millisecond);
        int num = r.Next(min, max);
        lblPickFive_1.Text = num.ToString();
        int num2 = r.Next(min, max);
        lblPickFive_2.Text = num2.ToString();
        int num3 = r.Next(min, max);
        lblPickFive_3.Text = num3.ToString();
        int num4 = r.Next(min, max);
        lblPickFive_4.Text = num4.ToString();
        int num5 = r.Next(min, max);
        lblPickFive_5.Text = num5.ToString();
    }


public void RandomNumber(int min, int max)
    {
        Random random = new Random();

        int num = random.Next(min, max);
        int num2 = random.Next(min, max);
        int num3 = random.Next(min, max);
        int num4 = random.Next(min, max);
        int num5 = random .Next(min, max);

        lblPickFive_1.Text = num.ToString();
        lblPickFive_2.Text = num2.ToString();
        lblPickFive_3.Text = num3.ToString();
        lblPickFive_4.Text = num4.ToString();
        lblPickFive_5.Text = num5.ToString();
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜