Results 1 to 9 of 9
- 02-06-2010, 10:57 PM #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[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); } }
-
You're biggest mistake is here:
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.Java Code:for (int random = 1; random <= r; random++) { int rand = (int) Math.random(); random = (rand + 1); }
- 02-08-2010, 04:01 AM #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:
-
What changes have you made to your program based on my recommendations above?
- 02-11-2010, 11:07 PM #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.
- 02-12-2010, 04:56 AM #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.
- 02-12-2010, 11:39 AM #7
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 DayLast edited by j2me64; 02-12-2010 at 11:41 AM.
- 02-12-2010, 02:22 PM #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.
- 02-12-2010, 02:38 PM #9
Similar Threads
-
problems generating random objects
By mandangalo in forum New To JavaReplies: 8Last Post: 07-30-2010, 04:42 AM -
Need help generating random numbers atleast 2 characters apart
By ruby&oliver in forum New To JavaReplies: 12Last Post: 09-23-2009, 09:14 AM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Generating a random number
By oridov in forum New To JavaReplies: 2Last Post: 11-29-2008, 05:12 PM -
generating random numbers in a 5x5 array.
By acidacid in forum New To JavaReplies: 3Last Post: 08-14-2007, 03:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks