By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,275 Members | 1,921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,275 IT Pros & Developers. It's quick & easy.

lotto 'style' random number generator with duplicate checking in C#

P: 1
My first app in 30 years ... how logically bad is it?
Lame lotto number generator but found it interesting enough to motivate me to code again ... lol. I would think there is a better way to check for duplicates and an easier way to display each 'ball' number into a different label. Thanks to any who might spend a moment to look this over
Expand|Select|Wrap|Line Numbers
  1.             // Initialize variable to point to each number in the array
  2.             int m = 0;
  3.  
  4.             // Loop until 5 random numbers have been generated and assigned to the array
  5.             for (int i = 0; i < mmar.Length; i++)
  6.             {
  7.                 // create arrays to utilize higher probablistic ranges for each ball
  8.                 int[] y = new int[5] { 1, 3, 8, 18, 30 };
  9.                 int[] z = new int[5] { 21, 31, 52, 63, 71 };
  10.                 // use the for loop's initialized variable to change random range
  11.                 mmnum = mmball.Next(y[i], z[i]);
  12.                 // Loop to ensure each number is unique
  13.                 for (int j = 0; j < mmar.Length; j++)
  14.                 {
  15.                     // check the number of ball for duplicates
  16.                     if(mmnum == mmar[j])
  17.                     {
  18.                         // if not unique create new ball
  19.                         mmnum = mmball.Next(1, 71);
  20.                         // re-initialize to ensure new ball number is unique
  21.                         j = 0;
  22.                     }
  23.                 }
  24.                 // put the ball number into our array
  25.                 mmar[m] = mmnum;
  26.                 // change to new array slot
  27.                 m++;
  28.             }
  29.  
  30.             // Sort the numbers in the array before displaying them
  31.             Array.Sort(mmar);
  32.             // OLD ... START Place lotto numbers on screen
  33.             num1mm.Text = mmar[0].ToString();
  34.             num2mm.Text = mmar[1].ToString();
  35.             num3mm.Text = mmar[2].ToString();
  36.             num4mm.Text = mmar[3].ToString();
  37.             num5mm.Text = mmar[4].ToString();
  38.  
4 Weeks Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.