3 <html lang=
"en" xmlns=
"http://www.w3.org/1999/xhtml">
5 <meta charset=
"utf-8" />
6 <style type=
"text/css">
16 vertical-align:bottom
;
17 background-color: #cb1d0b;
28 <script type=
"text/javascript">
30 function upsert(arr
, key
, value
) {
32 for (var i
= 0; i
< arr
.length
; ++i
) {
34 if (arr
[i
][0] == key
) {
35 arr
[i
][1].push(value
);
40 arr
.push([key
, [value
]]);
43 function upsert2(arr
, key
, value
, v2
) {
45 for (var i
= 0; i
< arr
.length
; ++i
) {
47 if (arr
[i
][0] == key
) {
54 arr
.push([key
, value
, [v2
]]);
57 function getCombinations(numDice
, sideValues
) {
60 for (var i
= 0; i
< sideValues
.length
; ++i
) {
61 vals
.push([sideValues
[i
]]);
66 var merge
= getCombinations(numDice
- 1, sideValues
);
68 for (var i
= 0; i
< vals
.length
; ++i
)
69 for (var j
= 0; j
< merge
.length
; ++j
)
70 result
.push(vals
[i
].concat(merge
[j
]));
78 function max(arr
, v
) {
79 v
= v
|| function (x
) { return x
}
80 return arr
.map(v
).reduce(function (a
, b
) { return a
> b
? a
: b
}, -Infinity
);
83 function min(arr
, v
) {
84 v
= v
|| function (x
) {return x
}
85 return arr
.map(v
).reduce(function (a
, b
, i
) { return a
< b
? a
: b
}, Infinity
);
89 return arr
.reduce(function (a
, b
) { return a
+ b
});
92 function getProbabilities(numDice
, sideValues
, numDropLeast
, numDropMost
)
94 var comb
= getCombinations(numDice
, sideValues
);
95 var prob
= Math
.pow(1 / sideValues
.length
, numDice
);
98 comb
.forEach(function (x
) {
102 for (var j
= 0; j
< numDropLeast
; ++j
) {
104 z
= z
.filter(function (y
) {
106 if (done
|| y
> min(z
)) return true;
113 for (var j
= 0; j
< numDropMost
; ++j
) {
115 z
= z
.filter(function (y
) {
116 if (done
|| y
< max(z
)) return true;
123 upsert2(xxx
, sum(z
), prob
, x
);
127 xxx
.sort(function (a
, b
) { return a
[0] - b
[0] });
129 xxx
.label
= numDice
+ 'd' + sideValues
.length
;
131 if (numDropLeast
> 0 && numDropMost
== 0)
132 xxx
.label
+= ' pick ' + (numDice
- numDropLeast
) + ' best';
134 if (numDropLeast
== 0 && numDropMost
> 0)
135 xxx
.label
+= ' drop ' + numDropMost
+ ' best';
137 if (numDropLeast
> 0 && numDropMost
> 0) {
138 xxx
.label
+= ' drop ' + numDropLeast
+ ' worst and' + numDropMost
+ ' best';
149 for (var k
= 0; k
< arguments
.length
; ++k
) {
151 var probabilities
= arguments
[k
];
153 for (var i
= 0; i
< probabilities
.length
; ++i
) {
154 maxprob
= maxprob
< probabilities
[i
][1] ? probabilities
[i
][1] : maxprob
;
156 console
.log(probabilities
[i
][0], probabilities
[i
][1]/*, probabilities[i][2].reduce(function (a, b) { return a + ' ' + b.join('+') }, '')*/);
161 for (var k
= 0; k
< arguments
.length
; ++k
) {
163 var probabilities
= arguments
[k
];
165 var e
= document
.createElement('div');
166 e
.className
= 'plot';
169 for (var i
= 0; i
< probabilities
.length
; ++i
) {
170 code
.push('<div class="bar" style="height: ' + (100 * probabilities
[i
][1] / maxprob
) + '%">' + probabilities
[i
][0] + '</div>');
173 code
.push('<div class="label">' + probabilities
.label
+ '</div>');
174 e
.innerHTML
= code
.join('');
175 document
.body
.appendChild(e
);
181 document
.body
.appendChild(document
.createElement('div'));
187 var sideValues
= [1, 2, 3, 4, 5, 6];
188 var numDropLeast
= 2;
191 render(getProbabilities(2, sideValues
, 0, 0)
192 , getProbabilities(3, sideValues
, 1, 0)
193 , getProbabilities(4, sideValues
, 2, 0)
194 , getProbabilities(5, sideValues
, 3, 0)
195 , getProbabilities(5, sideValues
, 2, 1)
198 render(getProbabilities(3, sideValues
, 0, 0)
199 , getProbabilities(4, sideValues
, 1, 0)
200 , getProbabilities(5, sideValues
, 2, 0)