Results 1 to 9 of 9
  1. #1
    a7x75 is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default 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[0]);
    		int r = Integer.parseInt(args[1]);
    		int g = Integer.parseInt(args[2]);
    		for (int i = 1; i <= w; i++)
    		{
    			if (w != g)
    			{	
    				System.out.println(w);
    			}	
    			else 
    			{
    				System.out.println("You already picked this number");
    			}
    		}
    		for (int random = 1; random <= r; random++)
    		{
    			int rand = (int) Math.random();
    			random = (rand + 1);
    		}
    	System.out.println("You picked numbers " + w + " and " + r);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    a7x75 is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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:

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What changes have you made to your program based on my recommendations above?

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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.

  6. #6
    a7x75 is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by a7x75 View Post
    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 11:41 AM.

  8. #8
    a7x75 is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by a7x75 View Post
    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.

Similar Threads

  1. problems generating random objects
    By mandangalo in forum New To Java
    Replies: 8
    Last Post: 07-30-2010, 04:42 AM
  2. Replies: 12
    Last Post: 09-23-2009, 09:14 AM
  3. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  4. Generating a random number
    By oridov in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 05:12 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Posting Permissions

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