Created
November 29, 2016 01:43
-
-
Save gregorp/86da164a2a900905689b8c0dc9fe35c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# p1 (1-10) is attribute - determines d: number of sides of dice | |
# p2 (1-10) is skill - determines k: number of dice to keep | |
# p1 + p2 determines n: the number of dice to roll | |
pp = expand.grid(p1 = 1:10, p2 = 1:10) | |
library(dplyr) | |
library(tidyr) | |
pp = pp %>% mutate( | |
n = p1 + p2, | |
d = ifelse(p1 < 4, 6, ifelse(p1 < 7, 8, 12)), | |
k = p2 | |
) | |
one_sim = function(n, d, k) { | |
roll = sort(sample(x = d, size = n, replace = TRUE), decreasing = T) | |
keep = head(roll, k) | |
total = sum(keep) | |
return(total) | |
} | |
many_sim = function(N, ...) { | |
result = integer(N) | |
for (i in 1:N) result[i] = one_sim(...) | |
return(c(mean(result), sd(result))) | |
} | |
results = matrix(0L, ncol = nrow(pp), nrow = 2) | |
for (i in 1:nrow(pp)) results[, i] = many_sim(N = 10000, n = pp$n[i], d = pp$d[i], k = pp$k[i]) | |
final = cbind(pp, t(results)) | |
names(final)[6:7] = c("mean", "sd") | |
## Complete output | |
final | |
# p1 p2 n d k mean sd | |
# 1 1 1 2 6 1 4.489 1.4177055 | |
# 2 2 1 3 6 1 4.964 1.1198026 | |
# 3 3 1 4 6 1 5.232 0.8859802 | |
# 4 4 1 5 8 1 7.157 1.1276625 | |
# 5 5 1 6 8 1 7.342 0.9131648 | |
# 6 6 1 7 8 1 7.398 0.8272124 | |
# 7 7 1 8 12 1 11.061 1.1818103 | |
# 8 8 1 9 12 1 11.182 1.0761199 | |
# 9 9 1 10 12 1 11.326 0.9595023 | |
# 10 10 1 11 12 1 11.365 0.9469275 | |
# 11 1 2 3 6 2 8.536 2.1291400 | |
# 12 2 2 4 6 2 9.428 1.9011655 | |
# 13 3 2 5 6 2 9.879 1.7097609 | |
# 14 4 2 6 8 2 13.577 1.9119955 | |
# 15 5 2 7 8 2 14.004 1.7880663 | |
# 16 6 2 8 8 2 14.263 1.5969914 | |
# 17 7 2 9 12 2 21.225 2.2251576 | |
# 18 8 2 10 12 2 21.608 2.1774015 | |
# 19 9 2 11 12 2 21.933 1.9514916 | |
# 20 10 2 12 12 2 22.082 1.8332039 | |
# 21 1 3 4 6 3 12.090 2.8106583 | |
# 22 2 3 5 6 3 13.337 2.6134769 | |
# 23 3 3 6 6 3 14.312 2.2697594 | |
# 24 4 3 7 8 3 19.429 2.9036857 | |
# 25 5 3 8 8 3 19.988 2.7245696 | |
# 26 6 3 9 8 3 20.648 2.4840021 | |
# 27 7 3 10 12 3 30.889 3.3671972 | |
# 28 8 3 11 12 3 31.609 3.1227986 | |
# 29 9 3 12 12 3 31.742 3.0067385 | |
# 30 10 3 13 12 3 32.158 2.7994415 | |
# 31 1 4 5 6 4 16.098 3.1941507 | |
# 32 2 4 6 6 4 17.471 3.2905907 | |
# 33 3 4 7 6 4 18.378 2.8455603 | |
# 34 4 4 8 8 4 25.212 3.5326386 | |
# 35 5 4 9 8 4 26.083 3.3674101 | |
# 36 6 4 10 8 4 26.557 3.1161613 | |
# 37 7 4 11 12 4 39.995 4.3400243 | |
# 38 8 4 12 12 4 40.881 3.9684490 | |
# 39 9 4 13 12 4 41.615 3.7880238 | |
# 40 10 4 14 12 4 41.884 3.7216118 | |
# 41 1 5 6 6 5 19.525 3.7836082 | |
# 42 2 5 7 6 5 21.144 3.6083631 | |
# 43 3 5 8 6 5 22.520 3.3824608 | |
# 44 4 5 9 8 5 30.263 4.5046779 | |
# 45 5 5 10 8 5 31.278 4.1590881 | |
# 46 6 5 11 8 5 32.479 3.7809860 | |
# 47 7 5 12 12 5 48.558 5.5226022 | |
# 48 8 5 13 12 5 49.675 5.3654602 | |
# 49 9 5 14 12 5 50.343 4.9521586 | |
# 50 10 5 15 12 5 51.042 4.5526874 | |
# 51 1 6 7 6 6 23.197 4.2351065 | |
# 52 2 6 8 6 6 25.080 4.1601571 | |
# 53 3 6 9 6 6 26.233 3.9960830 | |
# 54 4 6 10 8 6 35.630 4.9064420 | |
# 55 5 6 11 8 6 36.744 4.7851187 | |
# 56 6 6 12 8 6 38.000 4.4341472 | |
# 57 7 6 13 12 6 56.836 6.5371123 | |
# 58 8 6 14 12 6 58.368 5.8838079 | |
# 59 9 6 15 12 6 59.327 5.6332766 | |
# 60 10 6 16 12 6 59.995 5.5324120 | |
# 61 1 7 8 6 7 26.783 4.6724450 | |
# 62 2 7 9 6 7 28.512 4.6159683 | |
# 63 3 7 10 6 7 30.248 4.4614348 | |
# 64 4 7 11 8 7 40.377 5.8625285 | |
# 65 5 7 12 8 7 42.335 5.2959250 | |
# 66 6 7 13 8 7 42.881 5.1126293 | |
# 67 7 7 14 12 7 64.788 7.2146453 | |
# 68 8 7 15 12 7 66.069 7.0099485 | |
# 69 9 7 16 12 7 68.116 6.7260526 | |
# 70 10 7 17 12 7 68.706 6.4223680 | |
# 71 1 8 9 6 8 30.284 4.9292638 | |
# 72 2 8 10 6 8 32.249 4.8479379 | |
# 73 3 8 11 6 8 34.008 4.6959544 | |
# 74 4 8 12 8 8 45.755 6.1850813 | |
# 75 5 8 13 8 8 47.214 5.6458905 | |
# 76 6 8 14 8 8 48.638 5.5675806 | |
# 77 7 8 15 12 8 72.920 7.9637316 | |
# 78 8 8 16 12 8 74.901 7.5903104 | |
# 79 9 8 17 12 8 76.076 7.4187102 | |
# 80 10 8 18 12 8 76.893 7.3338487 | |
# 81 1 9 10 6 9 34.175 5.0862801 | |
# 82 2 9 11 6 9 35.939 5.2167512 | |
# 83 3 9 12 6 9 37.938 4.9669736 | |
# 84 4 9 13 8 9 50.797 6.4862827 | |
# 85 5 9 14 8 9 52.144 6.2615070 | |
# 86 6 9 15 8 9 54.031 5.6676416 | |
# 87 7 9 16 12 9 80.337 9.0038047 | |
# 88 8 9 17 12 9 82.625 8.4850676 | |
# 89 9 9 18 12 9 84.153 8.2842151 | |
# 90 10 9 19 12 9 85.645 7.8763578 | |
# 91 1 10 11 6 10 37.211 5.6345565 | |
# 92 2 10 12 6 10 39.090 5.3921216 | |
# 93 3 10 13 6 10 40.866 5.2820398 | |
# 94 4 10 14 8 10 55.379 6.9288793 | |
# 95 5 10 15 8 10 57.588 6.7997421 | |
# 96 6 10 16 8 10 59.006 6.3463565 | |
# 97 7 10 17 12 10 88.508 9.3845621 | |
# 98 8 10 18 12 10 90.436 9.1024589 | |
# 99 9 10 19 12 10 92.300 8.9128805 | |
# 100 10 10 20 12 10 93.028 8.8506242 | |
## Summarize just the mean: | |
cat(" p2\n"); round(tidyr::spread(final[c("p1", "p2", "mean")], key = p2, value = mean), 2) | |
# p2 | |
# p1 1 2 3 4 5 6 7 8 9 10 | |
# 1 1 4.49 8.54 12.09 16.10 19.52 23.20 26.78 30.28 34.17 37.21 | |
# 2 2 4.96 9.43 13.34 17.47 21.14 25.08 28.51 32.25 35.94 39.09 | |
# 3 3 5.23 9.88 14.31 18.38 22.52 26.23 30.25 34.01 37.94 40.87 | |
# 4 4 7.16 13.58 19.43 25.21 30.26 35.63 40.38 45.76 50.80 55.38 | |
# 5 5 7.34 14.00 19.99 26.08 31.28 36.74 42.34 47.21 52.14 57.59 | |
# 6 6 7.40 14.26 20.65 26.56 32.48 38.00 42.88 48.64 54.03 59.01 | |
# 7 7 11.06 21.23 30.89 39.99 48.56 56.84 64.79 72.92 80.34 88.51 | |
# 8 8 11.18 21.61 31.61 40.88 49.67 58.37 66.07 74.90 82.62 90.44 | |
# 9 9 11.33 21.93 31.74 41.62 50.34 59.33 68.12 76.08 84.15 92.30 | |
# 10 10 11.37 22.08 32.16 41.88 51.04 59.99 68.71 76.89 85.64 93.03 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment