I'm currently working through java for dummies and I have got to chapter 12. I'm doing one of the exercises which says to make a game which generates three random cards until the total is 21.
I have made the following code below which has the random number generator.
The strange thing is that no matter how many times I run it, the computer goes through the while loop 5 times and the first 4 times it's random but then on the fifth loop it generates 7 7 7 every time.
5 3 2
7 1 4
6 2 1
6 3 1
2 3 1
7 7 7
(numbers below the sevens are different every time I run the program but the 777 always comes on the sixth loop)
I'm totally baffled as to why this is because its supposed to be random. I would really appreciate any feedback any of you may have
import java.util.Random;
import static java.lang.System.out;
class SimpleDiceGame {
public static void main(String args[]) {
Random myRandom = new Random();
int card1 = 0, card2 = 0, card3=0;
while (card1 + card2 + card3 < 21) {
card1 = myRandom.nextInt(7) + 1;
card2 = myRandom.nextInt(7) + 1;
card3 = myRandom.nextInt(7) + 1;
out.print(card1);
out.print(" ");
out.print(card2);
out.print(" ");
out.println(card3);
}
}
}