Click here to Skip to main content
15,889,877 members
Articles / Desktop Programming / MFC
Article

Lottery Number Picker (Uses Random and Array)

Rate me:
Please Sign up or sign in to vote.
2.13/5 (7 votes)
18 Aug 2002 102K   16   14
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.


<BOLD>
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();
</BOLD>


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


Written By
Web Developer
United States United States
"..Been walking the earth since child-birth. I never went to school, and when I did I ate my homework. Never played it cool, and if I did I was a poser. You can't blame a nerd for trying to get closer. Now I'm the poster boy for geeks and freaks..."

My profile according to Google

I like tha moon

All Your Base Are Belong To Us

Think different, think beige.

Domopers

Disclaimer:

Any and all things I say, I in no way think myself better then anyone else. I have my fair share (well, a good deal) of problems. I just like to gripe whenever given the chance

Comments and Discussions

 
GeneralCycles 1000 Pin
mrhassell14-Feb-09 16:18
mrhassell14-Feb-09 16:18 
GeneralLotto Pin
Huseyin Altindag30-Apr-07 1:19
Huseyin Altindag30-Apr-07 1:19 
Generalcute.. Pin
f213-Oct-04 7:02
f213-Oct-04 7:02 
Questionplacing random numbers into an array? Pin
Manners19-Nov-02 10:03
Manners19-Nov-02 10:03 
AnswerRe: placing random numbers into an array? Pin
Christian Graus19-Nov-02 10:32
protectorChristian Graus19-Nov-02 10:32 
GeneralMake it real! Pin
Philip Fitzsimons20-Aug-02 4:41
Philip Fitzsimons20-Aug-02 4:41 
GeneralProgram Pin
Zachery20-Aug-02 4:32
Zachery20-Aug-02 4:32 
GeneralDid you?! Pin
Anonymous20-Aug-02 4:11
Anonymous20-Aug-02 4:11 
GeneralRe: Did you?! Pin
Anonymous14-Jul-05 11:35
Anonymous14-Jul-05 11:35 
GeneralIt doesn't pick winning numbers! Pin
David Wulff20-Aug-02 4:10
David Wulff20-Aug-02 4:10 
GeneralRe: It doesn't pick winning numbers! Pin
Chris Maunder20-Aug-02 4:27
cofounderChris Maunder20-Aug-02 4:27 
GeneralRe: It doesn't pick winning numbers! Pin
Mauricio Ritter20-Aug-02 5:43
Mauricio Ritter20-Aug-02 5:43 
GeneralRe: It doesn't pick winning numbers! Pin
Christian Graus19-Nov-02 10:34
protectorChristian Graus19-Nov-02 10:34 
GeneralRe: It doesn't pick winning numbers! Pin
ENewton1-Dec-03 11:05
ENewton1-Dec-03 11:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.