Search code examples
javaloopsdice

Rolling two dices until dice1 is larger than dice2 two times in java


The output of the dices are randomized and I created a function for that. I want to record how many "attempts" it took to get dice one to be larger than dice 2 two times in a row. So when it is larger two times in a row, I would like it to print ("it took" + trials + "for d1 to be larger than d2 two times in a row"). So far I have this but am stuck:

public static int Dice() {
  return (int)(Math.random() * 6.0) + 1;
}

public static void main(String[] args) {

              int d1 = Dice();
              int d2 = Dice();
              int trials = 0;

              for (int i = 0; i < 100000; i++) {
               if (t1 > t2) {
               trials++;
               if (t1 > t2) {
                trials++ 
               }
              }
             }

            System.out.println(trials);
    } 
} 

Solution

  • Welcome to SO! This should work. See the comments for a few things you were doing wrong and some explanation.

    public static int Dice() {
      return (int)(Math.random() * 6.0) + 1;
    }
    
    public static void main(String[] args) {
    
      // initialize variables to be updated in the loop
      int trials = 0;
      Boolean lastTimeD1WasGreater = false;
    
      for (int i = 0; i < 100000; i++) {
        // always increment the counter!
        trials++;
    
        // you had the wrong variable names here.
        // also they must be INSIDE the loop or else they would always come out the same
        int d1 = Dice();
        int d2 = Dice();
    
        if (d1 > d2) {
          if (lastTimeD1WasGreater) {
            // if it was greater both last time and this time then we're done!
            System.out.println("it took " + trials + " trials.");
            break;
          } else {
            // otherwise set this variable so we can be on the lookout next time.
            lastTimeD1WasGreater = true;
          }
        } else {
          // d1 was not greater, so we'll start looking for a pair in the next loop.
          lastTimeD1WasGreater = false;
        }
      }
    }