Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By JosAH

Thread: Random Generation Not being random

  1. #1
    Mmarzex is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default 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:
    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();
    
    }
    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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm wondering what RandomGenerator is and why you aren't using Random.

  3. #3
    Mmarzex is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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).

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You have not added any break statements to your cases in the switch.

  6. #6
    Mmarzex is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

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

    Quote Originally Posted by Junky View Post
    You have not added any break statements to your cases in the switch.
    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?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    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;
    		}
    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.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default

    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:

    Java Code:
    String[] slot= { "BAR", "BELL", "CHERRY",  "LEMON", "ORANGE",  "PLUM" };
    kind regards,

    Jos
    yellowledbet likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Mmarzex is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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:

    Java Code:
    String[] slot= { "BAR", "BELL", "CHERRY",  "LEMON", "ORANGE",  "PLUM" };
    kind regards,

    Jos
    That does seem to make more sense but I'm going through the book and doing everything in the ways it shows in the book and I have never seen how arrays work in java yet so I don't really know how to implement something like that.

Similar Threads

  1. Random shape generation
    By zeeextra in forum New To Java
    Replies: 0
    Last Post: 12-04-2009, 05:57 AM
  2. Trouble with Random number generation
    By SteroidalPsycho in forum New To Java
    Replies: 9
    Last Post: 11-16-2009, 11:15 PM
  3. Random number generation
    By toasty in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 11:41 AM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 03:43 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
  •