Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lottery Number Picker (Uses Random and Arrays)

Status
Not open for further replies.

Zachery

Programmer
Jul 10, 2002
5
US
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)
 
Zachery -

You may want to look into something like this:
Code:
using System.Security.Cryptography;

RNGCryptoServiceProvider MyRNG = new RNGCryptoServiceProvider;

Byte[] random = new Byte[100];
MyRNG.GetBytes(random);
This will get you 100 cryptographically strong random digits.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top