# Thread: Powerball Example (Generating Random Numbers)

1. Member Join Date
Feb 2010
Posts
10
Rep Power
0

## Powerball Example (Generating Random Numbers)

I have to do an assignment where I have to write a program that simulates the selection of Powerball lottery numbers. I have to use the Math.random() function to simulate the random selection of a numbered ball. There has to be five white balls and one red ball. The five white numbers must be unique and between 1 and 49. (After a ball is removed from the container, it cant be selected again.) The one red number must be between 1 and 42. The red number can possibly be the same number as one of the selected
white balls.

I'm so close to figuring this one out; the only issue is that when I enter a number, I get an endless loop (it compiles fine). I can't seem to figure out what's wrong with my code. Any advice? Thanks.

Java Code:
```import java.util.Random;
public class PowerBall
{
public static void main(String [] args)
{
int w = Integer.parseInt(args);
int r = Integer.parseInt(args);
int g = Integer.parseInt(args);
for (int i = 1; i <= w; i++)
{
if (w != g)
{
System.out.println(w);
}
else
{
}
}
for (int random = 1; random <= r; random++)
{
int rand = (int) Math.random();
random = (rand + 1);
}
System.out.println("You picked numbers " + w + " and " + r);
}
}```  Reply With Quote

2. ## You're biggest mistake is here:
Java Code:
```    for (int random = 1; random <= r; random++) {
int rand = (int) Math.random();
random = (rand + 1);
}```
Your loop index is an int called random, and you allow this index to be changed to a random number within the loop itself which will cause unpredictable behavior. Solution: in general don't do this, don't change the loop index (usually called "i") from within the loop itself.  Reply With Quote

3. Member Join Date
Feb 2010
Posts
10
Rep Power
0

## I made a mistake with my problem description; my program doesn't execute endlessly. It asks for three numbers when it should only ask for two, and whatever numbers I post, it just executes the first number by its amount (for example, if I input the numbers 4, 15, and 9, my output is:)

4
4
4
4

I know the number issue involves the three variable args at the top, but I need all three variables up there. I don't know why the output is doing what it's doing. :confused:  Reply With Quote

4. ## What changes have you made to your program based on my recommendations above?  Reply With Quote

5. ## I remember to have read this problem from mister rich raposa book "Java in 60 Minutes a Day" and here is his solution:

Java Code:
```public class Powerball
{
public static void main(String [] args)
{
int white1, white2, white3, white4, white5, red;

red = (int) (Math.random() * 42 + 1);

white1 = (int) (Math.random() * 49 + 1);
do
{
white2 = (int) (Math.random() * 49 + 1);
}while(white1 == white2);

do
{
white3 = (int) (Math.random() * 49 + 1);
}while(white1 == white3 || white2 == white3);

do
{
white4 = (int) (Math.random() * 49 + 1);
}while(white1 == white4 || white2 == white4 || white3 == white4);

do
{
white5 = (int) (Math.random() * 49 + 1);
}while(white1 == white5 || white2 == white5 || white3 == white5 || white4 == white5);

System.out.println("White balls: " + white1 + " " + white2 + " " + white3 + " " + white4 + " " + white5);
System.out.println("Red ball: " + red);
}
}```

I've solved the problem in a different way but I think his solution is quite self-explanatory. Hope this helps.  Reply With Quote

6. Member Join Date
Feb 2010
Posts
10
Rep Power
0

## I was able to figure it out, but thank you so much. My class is actually using Raposa's book (every assignment is their exercise). I'm going to have to ask you for all the answers haha jk.

My solution looked very similar after I got it. It makes a lot more sense after seeing it this way. My previous code made it way too complex than it had to be.  Reply With Quote

7. ##  Originally Posted by a7x75 My class is actually using Raposa's book (every assignment is their exercise).

I don't want you to become a cheater, but if allowed from your teacher you can download all solutions from here Wiley::Java in 60 Minutes A Day
Last edited by j2me64; 02-12-2010 at 12:41 PM.  Reply With Quote

8. Member Join Date
Feb 2010
Posts
10
Rep Power
0

## Wow, every solution, huh? I'm really grateful for the link. I don't plan on cheating my way through the class, but it's nice to have a reference guide so if I screw up a program I can figure out what I did wrong.  Reply With Quote

9. ##  Originally Posted by a7x75 so if I screw up a program I can figure out what I did wrong.
this is really the better way to learn. try to find your solution and then compare it to find yout what you could do better.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•