Like the title says, I'm having trouble trying to write out a function that will find the probability of finding the sum of a pair of dice 100 times to find the probability of each sum of 2,3,4,5...up to 12.
I've started with
counter[n_][0] = 0; n = 100;
Table[{x[i] = Random[Integer, {1, 6}], y[i] = Random[Integer, {1, 6}]
but I'm not sure how to write the If statement part in which the counter will add 1 for every-time the sum of x[i]+y[i]=2,3,4,5, or 6.
Answer
In Mathematica it is natural to approach such a task with list operations and pattern matching.
dice1 = RandomInteger[{1, 6}, 100];
dice2 = RandomInteger[{1, 6}, 100];
Count[dice1 + dice2, 2 | 3 | 4 | 5 | 6]
You seem to be a very new beginner, since you are using x[i] and y[i] as if these are vectors, when they are in fact not, in Mathematica. Mathematica is very different from other softwares, like MATLAB, so you can't just jump right into it without reading a tutorial first. Nevertheless, here is another solution:
Total@Boole[2 <= Total[#] <= 6 & /@ RandomInteger[{1, 6}, {100, 2}]]
Comments
Post a Comment