Skip to content

fix: 优化随机选择算法以确保概率均匀 #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jtcymc
Copy link

@jtcymc jtcymc commented Jun 20, 2025

  • 在选择随机元素时,使用 Math.floor() 替代 Math.round()
  • 此修改解决了随机元素概率不均的问题,提高了算法的公平性
  • 主要应用于人员抽奖和随机球数据生成两个场景
Math.round() 是四舍五入,那么:

0 被选中的范围是 [0, 0.5),宽度为 0.5

1 被选中的范围是 [0.5, 1.5),宽度为 1

最后一个数 n 被选中的范围是 [n - 0.5, n],宽度也是 0.5

➡️ 也就是说:首尾元素的选中概率是中间元素的一半!

generating-random-whole-numbers-in-javascript-in-a-specific-range
Math.floor才是Math.random的绝佳搭配

jtcymc added 2 commits June 20, 2025 22:58
- 在选择随机元素时,使用 Math.floor() 替代 Math.round()
- 此修改解决了随机元素概率不均的问题,提高了算法的公平性
- 主要应用于人员抽奖和随机球数据生成两个场景
在选择随机元素时,使用 Math.floor() 替代 Math.round()
此修改解决了随机元素概率不均的问题,提高了算法的公平性
主要应用于人员抽奖和随机球数据生成两个场景
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant