random模块
random.shuffle(你的列表)——有打乱列表内元素排列顺序,就地打乱返回None
random.sample(list, 8)从list中随机取8个
random.randint(self, a, b) Return random integer in range [a, b], including both end points.
random.choice(self, seq) Choose a random element from a non-empty sequence.
抽奖概率解决方案
各种相关信息可以记在数据库或配置变量或配置表
RATE = [25, 15, 9, 8, 3, 25, 5, 10]
def get_draw_index(cls, rate):
import random
"""随机变量的概率函数"""
start = 0
randnum = random.randint(1, sum(rate))
for index, item in enumerate(rate):
start += item
if randnum <= start:
break
return index
保证返回的三个完全不一样,这个方案感觉很low
def get_fail_random_data(cls, draw_obs):
import random
draw_obs.pop() # 这里要求未中奖必须在列表的最后
_random_obs = random.sample(draw_obs, 2)
_random_obs.append(random.choice(draw_obs))
_random_data = []
for _random_ob in _random_obs:
d = dict(kind=_random_ob.kind, num=_random_ob.kind)
_random_data.append(d)
return _random_data