Merge pull request #75 from jtcymc/main

fix: 优化随机选择算法以确保概率均匀
This commit is contained in:
LOG1997
2025-09-20 23:29:58 +08:00
committed by GitHub
2 changed files with 6 additions and 4 deletions

View File

@@ -32,7 +32,7 @@ export function addOtherInfo(personList: any[]) {
}
export function selectCard(cardIndexArr: number[], tableLength: number, personId: number): number {
const cardIndex = Math.round(Math.random() * (tableLength - 1))
const cardIndex = Math.floor(Math.random() * (tableLength - 1))
if (cardIndexArr.includes(cardIndex)) {
return selectCard(cardIndexArr, tableLength, personId)
}

View File

@@ -425,7 +425,8 @@ function startLottery() {
luckyCount.value = leftover < luckyCount.value ? leftover : luckyCount.value
for (let i = 0; i < luckyCount.value; i++) {
if (personPool.value.length > 0) {
const randomIndex = Math.round(Math.random() * (personPool.value.length - 1))
// 解决随机元素概率过于不均等问题
const randomIndex = Math.floor(Math.random() * (personPool.value.length - 1))
luckyTargets.value.push(personPool.value[randomIndex])
personPool.value.splice(randomIndex, 1)
}
@@ -587,8 +588,9 @@ function randomBallData(mod: 'default' | 'lucky' | 'sphere' = 'default') {
const cardRandomIndexArr: number[] = []
const personRandomIndexArr: number[] = []
for (let i = 0; i < indexLength; i++) {
const randomCardIndex = Math.round(Math.random() * (tableData.value.length - 1))
const randomPersonIndex = Math.round(Math.random() * (allPersonList.value.length - 1))
// 解决随机元素概率过于不均等问题
const randomCardIndex = Math.floor(Math.random() * (tableData.value.length - 1))
const randomPersonIndex = Math.floor(Math.random() * (allPersonList.value.length - 1))
if (luckyCardList.value.includes(randomCardIndex)) {
continue
}