Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Lottery Number Picker (Uses Random and Array)

0.00/5 (No votes)
18 Aug 2002 3  
A small little program that randomly picks six numbers and display the top six picked

Introduction

This program cycles through numbers 1-49 randomly c number of times. It then displays the top six picked numbers, in order. The Random is as random as possible, because it starts the cycle according to the computers millisecond clock. This may not be of much help to advanced programmers, but I think it's a neat little program.



c=1000;
int[] numb = new int[c];            
DateTime dt = new DateTime();
dt = DateTime.Now;
Random rnd = new Random(dt.Millisecond);
for (int i=0; i<c; i++)
{
  numb[ i ] = rnd.Next(1,49);
}
Array.Sort(numb);
int k=0;
int j=0;
int[] length = new int[50];
int[] result = new int[50];
for (int i=0; i<c-1; i++)
{
    if (numb[ i ] == numb[i+1])
           {
              result[k] = numb[ i ];
              length[k] = j+=1;
           }
    else
    {
        k+=1;
        j=0;
    }
}


Array.Sort(length, result);
txtNum1.Text = result[49].ToString();

txtNum2.Text = result[48].ToString();

txtNum3.Text = result[47].ToString();

txtNum4.Text = result[46].ToString();

txtNum5.Text = result[45].ToString();

txtNum6.Text = result[44].ToString();


How it works

It may be a little confusing, but here's the gist of it. Starting by the computers millisecond, numbers 1-49 are cycled through. They are then stored in an array, and sorted by number (lowest to highest). Then the second loop determines how many times a number appears in the array, that number is stored in another array and sorted (number appearing the least to number appearing the most). Then the lost six numbers of the array are displayed (being the six most picked)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here