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:
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();
}
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.
Thanks to anyone that reads this.