Lottery Generate 10,000 3 digit random numbers

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 13110 Views - Last Post: 01 November 2010 - 05:58 PM Rate Topic: -----

#1 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Lottery Generate 10,000 3 digit random numbers

Posted 14 October 2010 - 07:31 AM

Hi everyone,
I am seeking for some assistance in the Lottery program that suppose to generate 10,000 random numbers. I have two listbox which displays the random numbers. this is what is asking to do.

C# program that simulated a lottery game containing 3 digit numbers. This program was written as a Graphical User Interface application in Visual Studio .NET.
Your program generated multiple 3 digit random numbers and then counted how many times each 3 digit number appears.

Your Graphical user interface included:
A label that displays the 10 most common 3 digit numbers and the number of times it appeared
A label that displays the 10 least common 3 digit numbers and the number of times it appeared
A button that, when clicked, generates 10,000 random, 3 digit numbers and then populates the two labels mentioned above.

Your program also contained an array that kept track of the number of times each 3 digit value was generated
Your program contained a method that determines the 10 most common numbers
Your program contained a method that determines the 10 least common numbers

Can anyone please help?

public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }      

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            int n;
            int number;
            int count;
            int listBox;
             //Populate the array
            int[] lottery = new int[10000];

            //initialize the array
            for (n = 0; n < 10000; n++)
            {
                lottery[n] = new Random().Next(-1, 10000);
            }

            //Sort the array
            Array.Sort(lottery);

            //Count the number of times each number is found in the array
            number = lottery[0];
            count = 0;
            listBox = 1;
            for (n = 0; n < 10000; n++)
            {
                //if number is equal to the number in the current array position, increment count
                if (number == lottery[n])
                {
                    count++;
                }
                else
                {
                    if (listBox == 1)
                    {
                        lstNumbers1.Items.Add(number + "   " + count);
                        listBox = 2;
                    }
                    else
                    {
                        lstNumbers2.Items.Add(number + "   " + count);
                        listBox = 1;
                    }
                    count = 0;
                    number = lottery[n];
                }
            }


            
                
        }
    }
}



It renders the numbers 3 and 4 digits randomly with no errors.

Is This A Good Question/Topic? 0
  • +

Replies To: Lottery Generate 10,000 3 digit random numbers

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Lottery Generate 10,000 3 digit random numbers

Posted 14 October 2010 - 07:33 AM

What's the problem that you're having? Are there errors? If there are errors, please paste them in here so we can see what's going on.
Was This Post Helpful? 0
  • +
  • -

#3 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Lottery Generate 10,000 3 digit random numbers

Posted 14 October 2010 - 07:39 AM

View PostKilorn, on 14 October 2010 - 06:33 AM, said:

What's the problem that you're having? Are there errors? If there are errors, please paste them in here so we can see what's going on.


It generates some numbers 3 digit and 4 digit but has no errors it runs.
Attached is the program i dont know how to go about it.
It does generate the numbers but in 3 and 4 digit numbers. Subsequent clicks generate random numbers.
Thanks.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Lottery Generate 10,000 3 digit random numbers

Posted 14 October 2010 - 07:42 AM

You have the max value set to 10000 in this line: lottery[n] = new Random().Next(-1, 10000); One too many 0's.

EDIT: Also, since you only want 3 digit numbers, changing the max value to 999 would prevent the rare chance of the method returning 1000. lottery[n] = new Random().Next(-1, 999);

This post has been edited by Kilorn: 14 October 2010 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

#5 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Lottery Generate 10,000 3 digit random numbers

Posted 14 October 2010 - 07:26 PM

View PostKilorn, on 14 October 2010 - 06:42 AM, said:

You have the max value set to 10000 in this line: lottery[n] = new Random().Next(-1, 10000); One too many 0's.

EDIT: Also, since you only want 3 digit numbers, changing the max value to 999 would prevent the rare chance of the method returning 1000. lottery[n] = new Random().Next(-1, 999);


Thanks for your help.
I forgot all about the 3 digit yes i changed 10000 to 999. can you look at this pic?
This is what generates. Since I already have that, what I am trying to do is get a count of the 10 most common numbers. For some reason i can't get this working right.
If you look at the listboxes suppose to be labels though but using listboxes.
the numbers in the left listbox first column are the numbers and the second column is how many times it appears meaning the count of the numbers. But it also generates two and four digit weird?

Thanks.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#6 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Lottery Generate 10,000 3 digit random numbers

Posted 15 October 2010 - 01:56 AM

I think the 4 digits are generated via your count. This is due to the format of your string message in the listbox:

lstNumbers1.Items.Add(number + "   " + count); 



And thus, the 4 digits are the number of times your number occurs.

The two digit generation is due to the bound of your random number generation, this is from -1 till 999. If you really want a 3 digit generation, your bound has to be from 100 till 999.

Hope this helped you out. ^^
Was This Post Helpful? 0
  • +
  • -

#7 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Lottery Generate 10,000 3 digit random numbers

Posted 15 October 2010 - 07:36 PM

View Postkarabasf, on 15 October 2010 - 12:56 AM, said:

I think the 4 digits are generated via your count. This is due to the format of your string message in the listbox:

lstNumbers1.Items.Add(number + "   " + count); 



And thus, the 4 digits are the number of times your number occurs.

The two digit generation is due to the bound of your random number generation, this is from -1 till 999. If you really want a 3 digit generation, your bound has to be from 100 till 999.

Hope this helped you out. ^^


Based on my code I like to generate the 3-digit random numbers and count how many times it appears.
Like 10 most common numbers and 10 least common numbers.

I fixed the other for parameter for the 3-digit number.
Was This Post Helpful? 0
  • +
  • -

#8 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Lottery Generate 10,000 3 digit random numbers

Posted 15 October 2010 - 07:43 PM

this is what i am trying to accomplish.
i can't figure this thing out.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#9 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Lottery Generate 10,000 3 digit random numbers

Posted 21 October 2010 - 03:56 AM

Can anyone please assist me with this problem?
I greatly appreciate it.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Lottery Generate 10,000 3 digit random numbers

Posted 21 October 2010 - 04:29 AM

So how about you post your current code and what the current problem is? You've been told how to generate the 3-digit numbers.
Was This Post Helpful? 0
  • +
  • -

#11 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Lottery Generate 10,000 3 digit random numbers

Posted 28 October 2010 - 08:01 PM

View PostJackOfAllTrades, on 21 October 2010 - 03:29 AM, said:

So how about you post your current code and what the current problem is? You've been told how to generate the 3-digit numbers.


What i need to get and don't know how to generate is the 10 most common numbers and 10 least common numbers. 10 most common 3-digit number and 10 least common 3-digit numbers I have the 3-digit numbers working and generates 10,000 numbers. But i get larger numbers beside the 3-digit numbers.

public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }      
        //click the button successively to generate random numbers 
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            //declare my variables

            int number;
            int count;
            int listBox;
             //Populate the array for 10,000 numbers
            int[] lottery = new int[10000];
           
            //initialize the array
            for (count = 0; count < lottery.Length; count++)
            {
                lottery[count] = new Random().Next(100, 999);
                         
            }

            //Sort the array
            Array.Sort(lottery);


            //Count the number of times each number is found in the array
            number = lottery[0];
            count = 0;
            listBox = 1;
            for (count = 0; count < lottery.Length; count++)
            {
                //if number is equal to the number in the current array position, increment count
                if (lottery[count] == number)
                {
                    count++;
                }
                else
                {
                    if (listBox == 1)
                    {
                        lstNumbers1.Items.Add(number + " " + "\t" + count);
                        listBox = 2;
                    }
                    else
                    {
                        lstNumbers2.Items.Add(number + " " + "\t" + count);
                        listBox = 1;
                    }
                    count += number;
                    number = lottery[count];
                }

            }

            
                
        }
    }



The image will give you an idea what i am trying to accomplish.
The generate_3digit.jpg is what the school is showing how it should look.
The random_3digit.jpg is what I am getting but is incorrect.
I clicked on the button 10 times just to get the result.
Generates no errors. I have rewrote my program several times.
I appreciate for your help.
Thanks for your help.

Attached image(s)

  • Attached Image
  • Attached Image

This post has been edited by bravo659: 28 October 2010 - 08:04 PM

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Lottery Generate 10,000 3 digit random numbers

Posted 29 October 2010 - 03:21 AM

for (count = 0; count < lottery.Length; count++)
{
    //if number is equal to the number in the current array position, increment count
    if (lottery[count] == number)
    {
        count++;
    }
    else
    {
        if (listBox == 1)
        {
            lstNumbers1.Items.Add(number + " " + "\t" + count);
            listBox = 2;
        }
        else
        {
            lstNumbers2.Items.Add(number + " " + "\t" + count);
            listBox = 1;
        }
        count += number;
        number = lottery[count];
    }
}


For one thing, can you explain why you are incrementing count, which is your loop control variable, inside the loop's body?
Was This Post Helpful? 0
  • +
  • -

#13 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Lottery Generate 10,000 3 digit random numbers

Posted 29 October 2010 - 07:02 AM

Another (smaller) error in your code:
You asked to generate only 3 digit numbers and not 2 digit numbers. If you look in the list of lowest occurring numbers in your good example, one of the numbers is 086. So you should generate some 2 and 1 digit numbers. You just need to display them as three digits. Here is a snippet to show you what I mean.
string lotteryNumber;
int x = 123;
lotteryNumber = string.Format("{0:000}", x);
Console.WriteLine(lotteryNumber);
x = 086;
lotteryNumber = string.Format("{0:000}", x);
Console.WriteLine(lotteryNumber);
x = 0;
lotteryNumber = string.Format("{0:000}", x);
Console.WriteLine(lotteryNumber);

Was This Post Helpful? 0
  • +
  • -

#14 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Lottery Generate 10,000 3 digit random numbers

Posted 31 October 2010 - 08:39 PM

View PostJackOfAllTrades, on 29 October 2010 - 02:21 AM, said:

for (count = 0; count < lottery.Length; count++)
{
    //if number is equal to the number in the current array position, increment count
    if (lottery[count] == number)
    {
        count++;
    }
    else
    {
        if (listBox == 1)
        {
            lstNumbers1.Items.Add(number + " " + "\t" + count);
            listBox = 2;
        }
        else
        {
            lstNumbers2.Items.Add(number + " " + "\t" + count);
            listBox = 1;
        }
        count += number;
        number = lottery[count];
    }
}


For one thing, can you explain why you are incrementing count, which is your loop control variable, inside the loop's body?


I needed to get the count of the amount of times a 3-digit randomized number appears.
Was This Post Helpful? 0
  • +
  • -

#15 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Lottery Generate 10,000 3 digit random numbers

Posted 31 October 2010 - 08:44 PM

View PostRobin19, on 29 October 2010 - 06:02 AM, said:

Another (smaller) error in your code:
You asked to generate only 3 digit numbers and not 2 digit numbers. If you look in the list of lowest occurring numbers in your good example, one of the numbers is 086. So you should generate some 2 and 1 digit numbers. You just need to display them as three digits. Here is a snippet to show you what I mean.
string lotteryNumber;
int x = 123;
lotteryNumber = string.Format("{0:000}", x);
Console.WriteLine(lotteryNumber);
x = 086;
lotteryNumber = string.Format("{0:000}", x);
Console.WriteLine(lotteryNumber);
x = 0;
lotteryNumber = string.Format("{0:000}", x);
Console.WriteLine(lotteryNumber);

so i would need to add and declare a variable like a format in order to get a 3-digit number.
This is killing me. lol I been recoding forever. I need to get this one way or the other.
I have to try something. Not sure where i cam going so awry in my code.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2