Here is the program requirement and I need the most simple method since i am a beginner

and this is what i have so far

import java.util.Random;
class Lotto
{
public static void main (String[] args)
{
Random r = new Random();
int mynums[] = {6, 25, 31, 15, 42};
int lotto[] = {r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(42)+1};
int match;

So what's the question? What are you having trouble with?
Do you want me to write the application for you?

Now this is what i've got but I am stuck and you can do anything to help me if u want id greatly appreciate it...My program is due tomorrow and I am trying really hard but not getting the concept

Hi riabear123,

Let's take a look at your Lotto.java file and try to figure out what it's doing.

First, you import the Random class from java.util, because you need some way of generating random numbers. Then you set up the object-oriented overhead - the class and main method declarations. Then you get to the meat.

You create a Random object called 'r'. You can think of this little guy as a worker who will perform certain jobs upon request. One job he can perform is called '.nextInt()' - the idea behind this job is given in the documentation for the Random class:

nextInt(int n) 
     Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive), drawn from this random
number generator's sequence.

That's a little confusing. What it means is, if you create a Random object called r, and then say something like:

r.nextInt(5)

- r will put the numbers 0, 1, 2, 3, and 4 into a hat and draw one out at random, then return that number. It doesn't include 5 (that's what the word "exclusive" means in the documentation) and it will pull any of those five numbers with the same probability (that's what the term "uniformly-distributed" means in the documentation).

But don't take my word for it! After you've finished your program, try this out for yourself - it's the only way to learn how to code!

I should also note that the name '.nextInt(n)' is a little misleading, since all it does is generate a random number. You are probably wondering "nextInt? Next to what?" Don't worry about that right now - it has to do with the internal design of how a Random object picks elements randomly, and it really has little to do with your program.

So, keeping all that in mind, let's return to your program. After you create r, you create an array of integers called mynums[]. It has six slots, and those slots are filled by the numbers 6, 25, 31, 15, 42, and 36. Being a Lost fan, I prefer 4, 8, 15, 16, 23, and 42, but that's just me :cool: Then you create an integer variable called match, and set it equal to 0. Then you create six integer variables called lotto1, lotto2, lotto3, lotto4, lotto5, and lotto6. Each of these variables gets set equal to a random number - lotto1 through lotto5 get set equal to a random number between 1 and 50, and lotto6 gets set equal to a random number between 1 and 43. You get the random numbers by using successive calls to r.nextInt(n), where n is either 49 or 42, then adding one to shift the range.

This is all clear so far, right?

Now you enter a loop. The loop has an iterator called i, which increases by one after each iteration (cycle) of the loop. The loop only stops when i hits one million and one. In each iteration of the loop, you check the "lotto" variables against the entries in the "mynums" array. Whenever you find a match, you increase the match variable by one - except for the case when lotto1 is equal to mynums[0] - then, you increase match by two (I'm not sure if you really want to do this or not). Then you print out the value of match at this point.

First, let's make an observation: the way the program is currently written, you never change the lotto variables, and you never change the mynums array. Thus, each iteration of the loop makes the exact same checks of the exact same numbers. Are you sure you want to do this? Why not make the lotto variables change inside the loop? Wouldn't that be more like a real sequence of lotteries?

What's more, aren't you only supposed to win when all your numbers match up? The way you have it now, a match between any of your numbers and the corresponding lotto number will increase the value of match. Is this correct? Maybe you need to check all the numbers at once using the && operator, like so:

if (condition1 && condition2 && ... && conditionN) {
     // Do stuff
}

The thing in the parenthesis is often called a boolean expression - it's like an arithmetic expression, except that instead of resolving into a number, it resolves into either "True" or "False." Have you learned about these guys in class yet?

I would like to help more, but I can't until you tell me what the assignment asks you to do. I hope that I have at least helped clear up some of the concepts.

Thanks for taking time out to explain all that stuff..Here is what the program requirements are:

CSC 101
Program 3 - Lotto

It is said that if you buy a lot of lottery tickets it increases your chances of winning. Write a program that simulates buying a lot of tickets and add up how much money you would win.

The lottery rules are like this. You pick 6 numbers, hoping to guess the numbers that will be drawn. When the drawing happens, there are 5 numbers drawn randomly between 1 and 49 inclusive, and then one more drawn from 1-42. The first 5 are unordered, but the last one is special and has to be an exact match to the last choice. Here is a table showing how much money you get for various numbers of matches between your picks and the numbers drawn.

Match Payoff

5/1 Jackpot, let's say it's$35M
5/0 $100K
4/1 $5K
4/0 $100
3/1 $100
3/0 $7
2/1 $7
2/0 $4
1/1 $4
1/0 $1
0/1 $1

Your program should simulate buying a million tickets. You should assume that you use the same chosen numbers for all the tickets you buy. For each ticket, simulate the drawing and figure out how much money you won. Add this to a running total, and then buy the next ticket. At then end, say how much money you spent on tickets (1 ticket = 1 dollar) and how much money you won.

Extra Credit
Run the program using different amounts of tickets and different strategies for choosing your numbers and then make a recommendation for the best lotto playing strategy based on your programming results. This would need to be summarized in the comments section of Blackboard when you submit your program.


PS. I'm still really lost :sad:

Hi riabear123,

It might be helpful to consider a simpler problem (in general, it almost always is easier to understand a programming problem when you reduce it to its simplest form).

Imagine that you and I are playing a game - a very silly, pointless game. I write the numbers 0, 1, 2, 3, and 4 on five strips of paper and put them into a hat. You have a lucky number (let's say it's 3). The game is this: I pull a single strip of paper out of my hat. You guess that the number on that strip of paper is 3, your lucky number. If you are right, I give you a penny, then I put the strip of paper back in the hat. We play this exact same game a million times in a row.

Well, that game is a lot like your lottery program. Me pulling a number out of my hat is similar to you calling r.nextInt(5). The lucky number (3) is one of the entries in your mynums[] array. And the number of pennies you have at the end is just like the match variable. Let's think about how we would code up this simple version, and then you can try to tackle the complicated one on your own.

First off, what data does the program need to remember? It needs to remember three things: the lucky number, a random number, and the number of pennies you've won.

Now, what does the program have to do? If the program is supposed to look like us playing a million of these games, it will have to loop a million times, right? And whatever happens in each game will happen in each iteration of the loop (in other words, inside the loop). So what happens in one of our games? First, I pick a number at random. Then, we check to see if this number matches your lucky number. If it does, we add one to the total number of pennies you own. Otherwise, we do nothing. And we do this same thing a million times over. At the end of the million games, we look at all the pennies you've won.

That is the logic of our game. If we look at your code in Lotto.java, we see that it sort of looks like what I just described, except for two things:

1) You don't choose new random numbers inside the loop, like you should - you do it outside the loop.

2) You keep printing out the match variable inside the loop - that really ought to be outside the loop.

Does this make sense? Please try to code up the simple version. I would like to help you more, but you have to meet me halfway. If you can't code up the simple version, try to think of the reason why, and ask a specific question.

commented: Thumbs up. What an informative, well thought out post. Keep it up. :) +1
import java.util.Random;
class Lotto
{
public static void main (String[] args)
{
Random r = new Random();
int mynums[] = {4, 8, 15, 16, 23, 42};
int match=0;
int limit=5;



for (int i=0; i<=1000000; i++)
{
int lotto1 =r.nextInt(49)+1;
int lotto2 = r.nextInt(49)+1;
int lotto3 =r.nextInt(49)+1;
int lotto4 = r.nextInt(49)+1;
int lotto5 = r.nextInt(49)+1;
int lotto6 = r.nextInt(42)+1;
}


while (match<=limit)


System.out.println("I have spent $1,000,000 on lottery tickets ");
System.out.println("My total winnings are" + match);
}
}
}
}
}
}
}

can u help me compare my numbers to the random ones inside the loop

i would like to use a switch statement b/c it would make it easier then comparing my numbers against the random numbers with if statements or a for loop but im not sure how

Hi riabear123,

I would really prefer that you try to get the simple version first, because your logic is still a little screwed up. For instance, why do you have that limit variable up top? And why did you insert a while loop at the bottom? And why are all those end-curly braces in there? Also, could you please use the CODE tags to delineate blocks of code in the future? It helps other forum members and myself read them.

i had the limit variable up because i was going to have a counter, my teacher really cant teach and i dont know what the simple version is, i was to compare the numbers using a switch statement and then add the matches

i had the limit variable up because i was going to have a counter, my teacher really cant teach and i dont know what the simple version is, i was to compare the numbers using a switch statement and then add the matches

As much help as he's given you, you should be able to FULLY understand. I'm sorry, but you're just interested in having the program written for you.

Hi riabear123,

The "simple version" is the game I described upthread, where you and I are drawing numbers out of a hat, with you winning pennies every time you get a match. I really wouldn't worry about the logic behind the payoff system until you have completed this simpler problem.

However, in the interest of completeness, let me give you my take on what the assignment specification is asking:

The lottery rules are like this. You pick 6 numbers, hoping to guess the numbers that will be drawn. When the drawing happens, there are 5 numbers drawn randomly between 1 and 49 inclusive, and then one more drawn from 1-42. The first 5 are unordered, but the last one is special and has to be an exact match to the last choice. Here is a table showing how much money you get for various numbers of matches between your picks and the numbers drawn.

Match Payoff

5/1 Jackpot, let's say it's$35M
5/0 $100K
4/1 $5K
4/0 $100
3/1 $100
3/0 $7
2/1 $7
2/0 $4
1/1 $4
1/0 $1
0/1 $1

So, you have six numbers and the Random object is used to create six random numbers. The rules are:

1) Look at the first five of your own numbers. Count how many of those five numbers match against any of the six random numbers. Create a variable called x which is equal to this count.

2) Look at the last of your numbers. Does it match the sixth random number? Create a variable called y which is 1 if there is a match and 0 if there is not.

Now, x and y can be used to figure out your payoff. Go to the line in that table above which starts with "x/y." That line has your payoff.

Let's do an example to see how this works. Suppose that our numbers are 4, 8, 15, 16, 23, and 42. Suppose that the current lottery (random) numbers are 8, 1, 13, 2, 34, and 21. In this case, x will be 1 (because only one of the first five lucky numbers, 8, appears in both lists). And since the last numbers don't match, y will be 0. So we go to line 1/0 and see that our payoff is $1 - not much. Now, suppose that we keep the same lucky numbers and get lottery (random) numbers of 22, 15, 41, 8, 16, and 42. In this case, x will be 3 (because three of the first five lucky numbers, 15, 8, and 16, appear in both lists). And since the last numbers match, y will be 1. So we go to line 3/1 and see that our payoff is a whopping $100!

Is this clear? It's not that you're comparing the 2nd lucky number to the 2nd random number - you're comparing to all the random numbers, and you're supposed to do this for each of the first five lucky numbers.

I hope this exchange has helped. Truthfully, I would like to help you more, but I have a fair amount of work to do, myself. Try to code the simple version of the program - you'll get different amounts of pennies each time, but on average (given 5 strips of paper and 1,000,000 tries) you should get about 200,000 of them per execution of the program.

I attachted my program so far to this message. My questions are how do I make the y's and X that equal one to add up so I can figure out my winnings

import java.util.Random;
class Lotto
{
    public static void main (String[] args)
    {
        Random r = new Random();
        int mynums[] = {6, 11, 15, 36, 37, 40};
        int x=0;
        int y=0;
       int lotto[]= {r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(42)+1};  

        System.out.println("I have spent $1,000,000 on lottery tickets ");

        {
            if (y==1)
            {
             ++y;
            }
            else
            {
             if(y==0)
             {
                y=y+0;
             }
             }
        {





            if (lotto[0]==mynums[1])
            {
                y=1;
            }
            else
            {
            if(lotto[0]!=mynums[1])
            {
                y=0;
            }
            }
            if (lotto[0]==mynums[2])
            {
                y=1;
            }
            else
            {
            if(lotto[0]!=mynums[2])
            {
                y=0;
            }
            }
            if (lotto[0]==mynums[3])
            {
                y=1;
            }
            else
            {
            if(lotto[0]!=mynums[3])
            {
                y=0;
            }
            }
            if (lotto[0]==mynums[4])
            {
                y=1;
            }
            else
            {
            if(lotto[0]!=mynums[4])
            {
                y=0;
            }
            }

            if (lotto[1]==mynums[0])
            {
                y=1;
            }
            else
            {
            if(lotto[1]!=mynums[0])
            {
                y=0;
            }
            }
            if (lotto[1]==mynums[1])
            {
                y=1;
            }
            else
            {
            if(lotto[1]!=mynums[1])
            {
                y=0;
            }
            }
            if (lotto[1]==mynums[2])
            {
                y=1;
            }
            else
            {
            if(lotto[1]!=mynums[2])
            {
                y=0;
            }
            }
            if (lotto[1]==mynums[3])
            {
                y=1;
            }
            else
            {
            if(lotto[1]!=mynums[3])
            {
                y=0;
            }
            }
            if (lotto[1]==mynums[4])
            {
                y=1;
            }
            else
            {
            if(lotto[1]!=mynums[4])
            {
                y=0;
            }
            }
            if (lotto[2]==mynums[0])
            {
                y=1;
            }
            else
            {
            if(lotto[2]!=mynums[0])
            {
                y=0;
            }
            }
            if (lotto[2]==mynums[1])
            {
                y=1;
            }
            else
            {
            if(lotto[2]!=mynums[1])
            {
                y=0;
            }
            }
            if (lotto[2]==mynums[2])
            {
                y=1;
            }
            else
            {
            if(lotto[2]!=mynums[2])
            {
                y=0;
            }
            }
            if (lotto[2]==mynums[3])
            {
                y=1;
            }
            else
            {
            if(lotto[3]!=mynums[3])
            {
                y=0;
            }
            }
            if (lotto[3]==mynums[4])
            {
                y=1;
            }
            else
            {
            if(lotto[3]!=mynums[4])
            {
                y=0;
            }
            }
            if (lotto[4]==mynums[0])
            {
                y=1;
            }
            else
            {
            if(lotto[4]!=mynums[0])
            {
                y=0;
            }
            }
            if (lotto[4]==mynums[1])
            {
                y=1;
            }
            else
            {
            if(lotto[4]!=mynums[1])
            {
                y=0;
            }
            }
            if (lotto[4]==mynums[2])
            {
                y=1;
            }
            else
            {
            if(lotto[4]!=mynums[2])
            {
                y=0;
            }
            }
            if (lotto[4]==mynums[3])
            {
                y=1;
            }
            else
            {
            if(lotto[4]!=mynums[3])
            {
                y=0;
            }
            }
            if (lotto[4]==mynums[4])
            {
                y=1;
            }
            else
            {
            if(lotto[4]!=mynums[4])
            {
                y=0;
            }
            }

            }
            if (lotto[5]==mynums[5])
            {
                x=1;
            }
            else
            {
            if(lotto[5]!=mynums[5])
            {
                x=0;

            }
            }
     }
     }
     }

But you can see it better by just looking at the attachment

I have the program done now and i know its supposed to print out correctly but I have a small error somewhere that makes it not function. Can someone please check this attatchment?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.