代码:
// 1赢 0平 -1输
function win(m, n) {
if (Math.abs(m - n) === 2) {
return m < n ? 1 : -1;
} else {
return m - n;
}
}
function play(sum, m, n) {
var winCount = 0; // 总赢得次数
for (var i = 0; i < sum; i++) {
var ogwc = 0; // one game win count
for (var j = 0; j < m; j++) {
var a = Math.floor(Math.random() * 3);
var b = Math.floor(Math.random() * 3);
var status = win(a,b);
// console.log(a,b,status);
if (status === 1) {
ogwc++;
} else if (status === 0) { // 平局再来一次
j--;
}
if (ogwc === n) {
winCount ++;
break;
}
}
}
console.log(m + '局' + n + '胜,' + sum + '局,赢了' + winCount + '局,赢率:' + winCount / sum * 100 + '%');
}
play(100000, 1, 1);
play(100000, 3, 2);
play(100000, 5, 3);
play(100000, 7, 4);
play(100000, 11, 6);
//1局1胜,100000局,赢了50116局,赢率:50.11600000000001%
//3局2胜,100000局,赢了50132局,赢率:50.132%
//5局3胜,100000局,赢了50282局,赢率:50.282000000000004%
//7局4胜,100000局,赢了49827局,赢率:49.827%
//11局6胜,100000局,赢了50394局,赢率:50.394000000000005%
赢得概率一样高