From a173bff4b196fedc7fc2e49e3503c2fc22daafdc Mon Sep 17 00:00:00 2001 From: shaw <53377526+jtcymc@users.noreply.github.com> Date: Fri, 20 Jun 2025 22:58:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E7=AE=97=E6=B3=95=E4=BB=A5=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E6=A6=82=E7=8E=87=E5=9D=87=E5=8C=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在选择随机元素时,使用 Math.floor() 替代 Math.round() - 此修改解决了随机元素概率不均的问题,提高了算法的公平性 - 主要应用于人员抽奖和随机球数据生成两个场景 --- src/views/Home/index.vue | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/views/Home/index.vue b/src/views/Home/index.vue index e14d16c..9351c44 100644 --- a/src/views/Home/index.vue +++ b/src/views/Home/index.vue @@ -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 } From 87a02c080d164c5f8faa013d25b907db80fd2a69 Mon Sep 17 00:00:00 2001 From: shaw <53377526+jtcymc@users.noreply.github.com> Date: Sat, 21 Jun 2025 23:55:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E7=AE=97=E6=B3=95=E4=BB=A5=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E6=A6=82=E7=8E=87=E5=9D=87=E5=8C=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在选择随机元素时,使用 Math.floor() 替代 Math.round() 此修改解决了随机元素概率不均的问题,提高了算法的公平性 主要应用于人员抽奖和随机球数据生成两个场景 --- src/utils/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index a8caa4a..0e714f0 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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) }