mean(replicate(1000, throw(n)))And it will give you the mean of 1000 calls of throw(n). Of course you need to write a function that throws n dies and calculates the score...
score <- function(l)
{
y<-c();
for(x in 1:6) {
y=c(y,sum(l[l==x]))
};
max(c(y, max(y)))
};
throw <- function(n) {
score(sample(1:6, n, replace=T))
};
mean(replicate(10000,throw(10)))
Throws 10 dice 10,000 times, gives you the mean.
for n1=0 to N
for n2=0 to N-n1
for n3=0 to N-n1-n2
for n4=0 to N-n1-n2-n3
for n5=0 to N-n1-n2-n3-n4
n6=N-n1-n2-n3-n4-n5
calculate result for that combination of dice
calculate number of outcomes for that combination of dice and add to a tabulation
next n5
next n4
next n3
next n2
next n1
calculate required data (mean, median, mode, etc.)
A further optimization could be made by inverting the order of the loops—making n6 the outermost loop and n2 the innermost—and after each value is determined, check whether the remaining dice could possibly make a difference, and if not, skip the inner loops. E.g., after determining the number of 6's, calculate the sum of the 6's and ask if it would change the result if all remaining dice were 5's (if at least 5/11 of the dice are showing 6's, it would not), and if not, take the result right then and skip the inner loops. If so, then cycle through possible numbers of dice showing 5's, and for each one calculate the result based on 6's and 5's alone, ask if it would change the result if all remaining dice were 4's, and if not, skip the inner loops, and so forth. Of course, you need to alter the calculation for the number of results when you skip some of the inner loops.You are not logged in, either login or create an account to post comments
posted by escabeche at 6:34 PM on November 30, 2012 [1 favorite]