What am I doing wrong in this example?
I'm returning a random number between 1 and 4 using Random.nextInt(), however the method is returning more of the number 3 than the rest of them. I get consistent outputs like this:
NumOf1s: 767093, NumOf2s: 765836, NumOf3s: 1531645, NumOf4s: 765779
As you can see the NumOf3s is at 1.5M while the rest is at almost half. Anything in my code that is making this happen?
int numOf1s = 0, numOf2s = 0, numOf3s = 0, numOf4s = 0;
Random random = new Random();
int currentNum;
while(true) {
currentNum = random.nextInt(4) + 1;
switch (currentNum) {
case 1:
numOf1s++;
break;
case 2:
numOf2s++;
case 3:
numOf3s++;
break;
case 4:
numOf4s++;
break;
default:
break;
}
System.out.println("NumOf1s: "+ numOf1s + ", NumOf2s: "+ numOf2s + ", NumOf3s: "+ numOf3s + ", NumOf4s: "+ numOf4s);
}
Aucun commentaire:
Enregistrer un commentaire