Results 1 to 9 of 9
- 08-04-2011, 06:21 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
Random Generation Not being random
Hi all,
I'm currently going through Eric Roberts' The Art & Science of Java, which I'm finding a great book to give me an understanding of java and also cs in general before I actually start college. Anyway I'm currently going through some of the problem sets right now and one of the problems is trying to simulate a slot machine where all possible symbols have an equal chance of hitting. Now the program isn't done yet. I haven't added what I was thinking of for checking for winning sets and such as I noticed an issue with the random generation of the row of three symbols. For whatever reason no matter how many times I run it and try it. The random generator for all three rows returns the result plum plum plum.
Here is my code:
Like I said I'm not entirely sure why it is that it returns the same result every time. From my understanding of how I have it set up. In the slotRoll method I have it set that it randomly picks a number 1 through 6 for r1, r2, and r3. And each are then passed on to a switch statement respectively and then the resulting string should be passed to s1, s2, and s3. Then I print s1, s2, and s3 through the println call. I thought that my issue may be the way they are listed in the println but that is not the case when I print them seperately and they all give me plum. I have no idea why at this point.Java Code:import acm.program.*; import acm.util.*; public class SlotMachine extends ConsoleProgram { public void run() { int cash = STARTING_CASH; playSlots("You have " + cash + "Would you like to play"); } private boolean playSlots(String prompt) { while (true) { String reply = readLine(prompt); if(reply.equals("yes")) { Slotroll(); } else if(reply.equals("no")) { println("Fine don't gamble."); } } } private String Slotroll() { String bar = ("BAR"); String bell = ("BELL"); String cherry = ("CHERRY"); String lemon = ("LEMON"); String orange = ("ORANGE"); String plum = ("PLUM"); String s1 = (""); String s2 = (""); String s3 = (""); int r1 = rgen.nextInt(1, 6); int r2 = rgen.nextInt(1, 6); int r3 = rgen.nextInt(1, 6); switch (r1) { case 1: s1 = bar; case 2: s1 = bell; case 3: s1 = cherry; case 4: s1 = lemon; case 5: s1 = orange; case 6: s1 = plum; } switch (r2) { case 1: s2 = bar; case 2: s2 = bell; case 3: s2 = cherry; case 4: s2 = lemon; case 5: s2 = orange; case 6: s2 = plum; } switch (r3) { case 1: s3 = bar; case 2: s3 = bell; case 3: s3 = cherry; case 4: s3 = lemon; case 5: s3 = orange; case 6: s3 = plum; } String result = (s1 + " " + s2 + " " +s3); println(result); return result; } private static final int STARTING_CASH = 50; private RandomGenerator rgen = RandomGenerator.getInstance(); }
Thanks to anyone that reads this.
- 08-04-2011, 06:35 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I'm wondering what RandomGenerator is and why you aren't using Random.
- 08-04-2011, 06:39 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
Oh Sorry. I keep forgetting that many of the classes and such in this book are from the acm library and that most people do not use them. Anyway RandomGenerator according to the book and from my understanding is just a subclass of Random. I don't really know of the differences between the two though.
- 08-04-2011, 06:59 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ignore everything I've said previously. Your problem lies in how you handle the switch statements. I suggest you read up on switch statements(especially fall through) and see if you can figure out what is wrong(the entire problem lies in the switch statements).
- 08-04-2011, 07:00 AM #5
You have not added any break statements to your cases in the switch.
- 08-04-2011, 02:11 PM #6
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
Yeah thanks I realized that I didn't put in break statements after each of the cases in the switch statements. For whatever reason at first I was thinking they weren't needed.
You sir are correct. Thanks.
Also I understand now the need for the break statements but why was it that it kept just giving me the result as plum? I'm going out on a limb but was it because it just cycled through the whole set of cases and ended on the last?
- 08-04-2011, 03:13 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Without the break then, should r1 = 1, it will go and run that first line (setting s1 to bar). It will then (since there is no break to tell it to stop) go on to the next line and set s1 to bell...and so on until finally setting s1 to plum. Switches fall through to successive cases unless they encounter a break.Java Code:switch (r1) { case 1: s1 = bar; case 2: s1 = bell; case 3: s1 = cherry; case 4: s1 = lemon; case 5: s1 = orange; case 6: s1 = plum; }
- 08-04-2011, 03:27 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Just a remark: I wouldn't use a switch statement at all; I'd generate a pseudo random number in the range [0,6) and pick an element from this array:
kind regards,Java Code:String[] slot= { "BAR", "BELL", "CHERRY", "LEMON", "ORANGE", "PLUM" };
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-04-2011, 10:53 PM #9
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Random shape generation
By zeeextra in forum New To JavaReplies: 0Last Post: 12-04-2009, 05:57 AM -
Trouble with Random number generation
By SteroidalPsycho in forum New To JavaReplies: 9Last Post: 11-16-2009, 11:15 PM -
Random number generation
By toasty in forum New To JavaReplies: 1Last Post: 09-30-2009, 11:41 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 -
random generation
By carlos123 in forum New To JavaReplies: 10Last Post: 01-09-2008, 03:43 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks