0

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);      
    }
  } 
}
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
John
  • 21
  • 5
  • 2
    When I run your code I get the 7,7,7 after a random number of attempts. Are you sure this is the full code you are running? – Rick Feb 10 '21 at 16:55
  • 2
    Please take some time to format your code - it's very hard to read in its current form. – Jon Skeet Feb 10 '21 at 17:02
  • If you use a value larger than 7 when calling `myRandom.nextInt()`, then you'll get different final values. At the moment `7 7 7` is the _only_ way the loop will terminate. – splrs Feb 10 '21 at 19:29

3 Answers3

2

You only stop when the sum of all three numbers is 21 or bigger, each number can be at max 7 - therefore you stop whenever all three numbers are 7 and the sum is 21.

Note that the output does not always happen on the n-th loop, it may just have been in your case, but that is how randomness works. Run the code over and over again to verify that.

luk2302
  • 55,258
  • 23
  • 97
  • 137
1

How are you counting the repetitions? The inner loop always stops when the sum == 21 but the iteration count of the do/while loop for that to occur varies from run to run.

for (int i = 0; i < 10; i++) {
    int count = 0;
    int card1 = 0, card2 = 0, card3 = 0;
    do {
        card1 = myRandom.nextInt(7) + 1;
        card2 = myRandom.nextInt(7) + 1;
        card3 = myRandom.nextInt(7) + 1;
        count++;
    } while (card1 + card2 + card3 < 21);
    System.out.println(count);
}

Prints something like the following:

1486
708
345
517
245
798
222
34
561
375
WJS
  • 36,363
  • 4
  • 24
  • 39
  • I count the repetitions by seeing how many times it prints the outcome until it hits 7 7 7 which for some reason is always 6 times – John Feb 14 '21 at 14:57
  • When I run your code I get something similar to what I printed above. Perhaps you should edit your question and show how you are counting. – WJS Feb 14 '21 at 15:02
1

As pointed out by other answers, you might be "coincidentally" reaching the sum of 21 on the n-th loop every time you run the program.

General note:
Random uses an initial seed to generate the numbers. The mentioned behavior is only possible if same seed is specified while creation of Random instance, in every run. In that case you will reach your target of 21 in same number of loops every time you run the program. Note that the pattern of generated numbers will also be the same in each run.
Instantiating with the seed value:

Random myRandom = new Random(2); // initial seed value = 2  

Check this out: Java random numbers using a seed

Yashwanth
  • 86
  • 5