// 乱序
function shuffle(a) {
for(let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i)
[a[i - 1], a[j]] = [a[j], a[i - 1]]
}
return a
}
// 排序
function bubbleSort(array) {
const len = array.length
if (len < 2) return array
for (let i = 0; i < len; i++) {
for (let j = 0; j < i; j++) {
if (array[j] > array[i]) {
const temp = array[j]
array[j] = array[i]
array[i] = temp
}
}
}
return array
}
const prize = () => {
const a = new Array(8).fill('a')
const b = new Array(8).fill('b')
const c = new Array(8).fill('c')
// const box = shuffle(a.concat(b).concat(c))
const box = a.concat(b).concat(c)
const result = []
for (let i = 0; i < 12; i++) {
const r = Math.floor(Math.random() * 24)
result.push(box[r])
}
const aNum = result.filter(item => item === 'a').length
const bNum = result.filter(item => item === 'b').length
const cNum = result.filter(item => item === 'c').length
return [aNum, bNum, cNum]
}
const count = 10000
const resultArr = [
[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6],
[1, 1], [1, 2], [1, 3], [1, 4], [1, 5],
[2, 2], [2, 3], [2, 4], [2, 5],
[3, 3], [3, 4],
[4, 4]
]
const result = resultArr.map(item => {
return {
number: `${item.toString()},${12 - item[0] - item[1]}`,
count: 0
}
})
for (let i = 0; i < count; i++) {
const r = prize()
for (const item of result) {
if (item.number === bubbleSort(r).toString()) {
item.count++
}
}
}
for (const item of result) {
console.log(item.number, Math.floor((item.count / count) * 10000) / 100 + '%')
}
和李老师计算的概率不一致