订单号首位按概率生成
随机100w订单统计的成功结果:
fst_char_oid count(*) 1 10 2 193 3 228 4 2227 5 5605 6 1834
目的:按照这种规律生成订单号的首位 注:总数10097,5开头的5605,占总数的55.6%
def first_num(): import random ran_list = [10, 193, 228, 2227, 5605, 1834] for i in range(len(ran_list), 1, -1): ran_list[i - 1] = sum(ran_list[:i]) a = random.randrange(0, 10097) for i in range(len(ran_list)): if a < ran_list[i]: break return i+1 s=[first_num() for i in range(100000)] print(len([ x for x in s if x ==5])/100000) result: 运行三次的值分别是:0.55668,0.55515,0.55464,均比较接近55.6%