概述
如何公平的抽奖?
简化一下问题,我们不考虑任何权重,就单纯的从50个人的池子里,随机选中5个人中奖。。
就类似于幼儿园摇号一样的情景。
各位觉得怎么样做才能算公平呢?
我自己写了个python的,感觉也不算绝对的公平..
// 请把代码文本粘贴到下方(请勿用图片代替代码)
import random
# craete a lists, sample 50 person
total = 50
# to ensure the process will cannot cheat, may add some secure method, like md5(username+ some real random info + slat),
# use it as user info, so the code admin will only see some md5 info, but not the user's name stuff
# here for demo, just use number to represent a person
persons_id = range(1, total + 1)
print(persons_id)
def getRandomSeed(i):
# get a int random nubmer
seed = generateRandomint()
# if this seed not taken
if checkSeed(seed):
result = {"personID": i, "seed": seed}
return result
else:
# if already taken, recursive call it self
return getRandomSeed(i)
# record the seeds
seed_lists = []
def checkSeed(seed):
# check if the seed already taken or not.
if seed not in seed_lists:
seed_lists.append(seed)
return True
else:
return False
def generateRandomint():
return random.randint(1, total)
# generate random int number for every person
persons_pool = map(getRandomSeed, persons_id)
# now, we can pick a range, like say, before the process, we all agreed that seed number is between 20 and 25 are the winners.
# so if your seed is in this range, you win.
print(persons_pool)
希望朋友们指点一下,到底怎么实现一个绝对公平的抽奖?
最后
以上就是粗犷书包为你收集整理的python 抽奖 完全公平的随机数算法_如何最公平的进行抽奖?代码实现的全部内容,希望文章能够帮你解决python 抽奖 完全公平的随机数算法_如何最公平的进行抽奖?代码实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复