Random.nextInt() always returning same number? [Solved]

For some reason, whenever i call the statement “random.nextInt(5)”, it always returns 4. But only in two of the methods that i have, otherwise, it works correctly. The two methods that don’t work are as follows.

	public String getRandomDeathMessage(){
		
		int c = random.nextInt(5);
		String ret = "dead";
		switch(c){
		case 0:ret = "You got dead.";
		case 1:ret = "You done died, son.";
		case 2:ret = "How's the dying going?";
		case 3:ret = "I admit, you should've made that.";
		case 4:ret = "Nice one, slab.";
		}
		return ret;
	}

The second method is essentially a clone of this one, only a different variable.
It always returns “Nice one, slab.”
Does anyone know why, or what i could do to fix it?

Ough… bad mistake :frowning:

Common mistake with newcomers, and common mistake with odies as well. You simply often forget it…

Every case needs a [icode]break;[/icode] if you don’t want the other cases to “execute”.
(or a return)

yeah, i noticed that immediately after i posted it… I’ve made this mistake before…
I tried to delete the post, but it wouldn’t let me.

I know you already know the answer, but the question exists anyways:
So why the f*ck is that?
Simply said, a common quote: “It’s not a bug. It’s a feature!”.

Let’s write a method which returns the number of days a month has. Usually you’d do this:


public static int getDaysOfMonth(String month) {
	// Using a String-switch from Java 7 here.
	// If that wouldn't be available, you'd usually use 
	// "public static final *MONTH*"'s.
	switch (month) {
	case "January": return 31;
	case "February": return whatever();
	case "March": return 31;
	case "April": return 30;
	case "May": return 31;
	case "June": return 30;
	case "July": return 31;
	case "August": return 30;
	case "September": return 31;
	case "November": return 30;
	case "December": return 31;
	}
}

You can do this much slicker with the multi-case:


public static int getDaysOfMonth(String month) {
	switch (month) {
	case "January": case "March": case "May": case "July": case "September": case "December":
		return 31;
	case "April": case "June": case "August": case "November":
		return 30;
	case "February":
		return mightyMagic();
	}
}

So that’s why it’s like it is :slight_smile:
I know you already solved it, but I hope this makes it clearer anyways :slight_smile:

In advanced cases (har har) you can also partly execute one case, then fall through to the next one. I really recommend not doing that sort of thing though; there’s almost always better ways to express it.

The main reason Java’s case statement behaves the way it does because that’s how C did it. In C’s case, case labels are very bare syntax sugar for a goto-style unconditional jump, which leads to all kinds of happy fun abuse like Duff’s Device (FYI, it doesn’t work in Java, and it’s obsolete for C). But at least Tom Duff also gave us half of Porter-Duff, so he wasn’t completely evil :slight_smile:

Think: State-machines.

Edit the first post and add [Solved] to the beginning of the title instead.