I’m working on Project Euler problem number 205 which states:
Peter has nine four-sided (pyramidal)
dice, each with faces numbered 1, 2,
3, 4. Colin has six six-sided (cubic)
dice, each with faces numbered 1, 2,
3, 4, 5, 6.Peter and Colin roll their dice and
compare totals: the highest total
wins. The result is a draw if the
totals are equal.What is the probability that Pyramidal
Pete beats Cubic Colin? Give your
answer rounded to seven decimal places
in the form 0.abcdefg
My initial attempt (below) involved having 1,000 “games”, where each game had 1,000,000 turns. Then taking the average of all the games. I’m consistently getting results in the .559 area, but when the answer needs to be to 7 decimal places, that’s not that close.
public class pe205 {
public static void main(String[] args) {
pe205 p = new pe205();
double sum = 0.0;
for(int i=0; i < 1000; i++){
sum += p.determineProbability();
}
System.out.println(sum/1000.0);
} // end main
public double determineProbability(){
int peterWins = 0;
int colinWins = 0;
for(int i=0; i < 1000000; i++){
int peterSum = 0;
for(int j=0; j < 4; j++){
Random r = new Random();
peterSum += r.nextInt(9);
}
//System.out.println(peterSum);
int colinSum = 0;
for(int j=0; j < 6; j++){
Random r = new Random();
colinSum += r.nextInt(6);
}
//System.out.println(colinSum);
if(peterSum > colinSum){
peterWins++;
}
if(colinSum > peterSum){
colinWins++;
}
}
double peteBeatsColin = (double)peterWins/(double)(colinWins + peterWins);
return peteBeatsColin;
}
} // end class
I’ve read about the Monte Carlo method. Would this be a situation where that would be useful, and if so, could someone give me a brief walk through? Or is it that I’m missing some fairly obvious mathematical solution?
I would like to say that I enjoy the challenge of these problems, and I’m not looking for the answer, just a little push in the right direction.
Why not try to calculate the result combinatorially? Explicitly calculate the result by adding terms of the form