1

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 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)
       {

       }
    }
}
Community
  • 1
  • 1
user770022
  • 2,899
  • 19
  • 52
  • 79
  • 7
    Dude this has to be a record, how many times can you ask a question about the same piece of code in one day. – Gabe Oct 15 '10 at 19:14
  • I do appologize for asking so many questions. But I am sure you didnt learn this the first time. – user770022 Oct 15 '10 at 19:18
  • 2
    @gmcalab: LOL, didn't even know that, I was referring to his usage of `Random`. – leppie Oct 15 '10 at 19:18
  • 2
    I already commented in your previous question: "And you should only create one instance of Random and call .Next on it for each random number. Else getting two random numers quickly after each other is problematic since Random is seeded with the time." – CodesInChaos Oct 15 '10 at 19:31
  • Me too. This is like deja-vu. – John Alexiou Oct 15 '10 at 19:34
  • My comment from your previous question... FWIW, you're effectively shuffling cards and taking the first N "cards" for your numbers (not familiar with all the lotteries listed but Powerball would have two decks -- white balls and the Powerball). If you go this route, you'll spend no time checking for duplicates (the random number approach can lead to dupes) – Austin Salonen Oct 15 '10 at 19:50
  • @randywhite30 - Back in my day, we had to use Google to learn about programming. Joking aside, we welcome good questions on SO. Take a moment to learn what makes a good question http://tinyurl.com/so-hints – Greg Oct 15 '10 at 19:52

4 Answers4

5

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();
    }
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
3

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.

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
  • All the solutions here address the misuse of random but don't address the problem of unwanted duplicates -- this does and likely answers your next question if you continue down the `Random.Next()` path. – Austin Salonen Oct 15 '10 at 20:16
1

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();
    }
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
0
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();
    }
bevacqua
  • 47,502
  • 56
  • 171
  • 285