1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/10/14 14:27 # @Author : Ropon # @File : 18_01.py
#常用模块一 # random # time # os # sys # 序列化模块 # json # pickle # collections 数据类型扩展,面向对象进阶
import random
# 取随机小数:数学计算 # print(random.random()) #取0-1之间的小数 # print(random.uniform(1, 2)) 取1-2之间的小数
# 取随机整数:彩票 抽奖 # print(random.randint(1, 2)) #[1, 2] 顾头也顾尾 # print(random.randrange(1, 2)) #[1, 2) 顾头不顾尾 # print(random.randrange(1, 100, 2)) #[1, 3, 5, 7 ...) 顾头不顾尾
# 从一个列表中随机取值:抽奖 # lst = ['a', 'b', (1, 2), 123] # print(random.choice(lst)) # print(random.sample(lst, 2)) #随机取2个不重复的值
# 打乱一个列表顺序,在原列表基础上修改,节省空间:洗牌 # random.shuffle(lst) # print(lst)
# 验证码 # 4位数字验证码 # 6位数字验证码 # 6位数字+字母验证码
# 4位或6位数字验证码 # def func(n=6): # s = '' # for i in range(n): # num = (random.randint(0, 9)) # s += str(num) # return s # # print(func()) # print(func(4))
# 6位数字+字母验证码 # print(chr(65)) #65+25 97+25 # print(chr(97)) # def func(n=6): # s = '' # for i in range(n): # num = str(random.randint(0, 9)) # alpha_upper = chr(random.randint(65, 90)) # alpha_lower = chr(random.randint(97, 122)) # res = random.choice([num, alpha_upper, alpha_lower]) # s += res # return s # # print(func()) # print(func(4))
# 4位数字验证码 # 6位数字验证码 # 6位数字+字母验证码
# def func(n = 6, alpha = True): # s = '' # for i in range(n): # num = str(random.randint(0, 9)) # if alpha: # alpha_upper = chr(random.randint(65, 90)) # alpha_lower = chr(random.randint(97, 122)) # num = random.choice([num, alpha_upper, alpha_lower]) # s += num # return s # # print(func()) # print(func(4,False))
# 发红包 # 红包数量 钱数 # 拼手机红包
# def hongbao(amount=5, count=3): # s = [] # min = 1 #分 # amount = amount * 100 #单位分 # max = amount - (count-1) #单位分 # for i in range(count-1): # # num = random.randint(0, amount) # num = random.uniform(0, int(amount)) # while num < min or num > max: # num = random.uniform(0, int(amount)) # amount -= num # s.append(round((num / 100), 2)) # s.append(round((amount / 100), 2)) # random.shuffle(s) # return s
# def hongbao(amount=5, count=3): # s = [] # min = 1 #分 # amount = amount * 100 #单位分 # max = amount - (count-1) #单位分 # for i in range(count-1): # num = random.randint(0, amount) # # while num < min or num > max: # # num = random.randint(0, amount) # amount -= num # s.append(round((num / 100), 2)) # s.append(round((amount / 100), 2)) # random.shuffle(s) # return s # # print(hongbao(1, 6))
# 0--------------------------------20 # 0 2 4 10 16 20
# def hongbao(amount=10, count=5): # ret = random.sample(range(1, amount * 100), count - 1) #分为单位 # ret.extend([0, amount * 100]) #追加多个值0 amount * 100 # ret.sort() #排序 # # return [((ret[i+1] - ret[i]) / 100 for i in range(num))] # 列表生产式 # for i in range(count): # yield (ret[i+1] - ret[i]) / 100 # # res = hongbao(10, 6) # for i in res: # print(i)
# def redbags(money, num=10): # choice = random.sample(range(1, money * 100), num - 1) # choice.extend([0,money*100]) # choice.sort() # return [(choice[i + 1] - choice[i]) / 100 for i in range(num)] # print(redbags(100,10))
# def hongbao(money,n): # k=n # sum=0#sum为前n个人抢得的总和,为了方便计算最后一个人的金额,初始值为0 # round=n#剩余人次 # while k>1: # current_money = money # 当前剩余的钱,初始值为money # for i in range(1,n+1): # get_money=random.randint(0,int(2*current_money/round)) # print('id[%s] have geted money %s'%(i,get_money)) # current_money -= get_money # round -= 1 # sum += get_money # k-=1 # # # if k==1:#最后一个人,分得剩余的所有 # print('id[%s] have geted money %s'%(n,money-sum)) # print(current_money) # print(hongbao(100,10))
def hongbao(amount=5, count=3): s = [] sum = 0 #分 amount = amount * 100 #单位分 for i in range(count-1): #0 1 2 max = amount -sum - (count - 1 - i) # 单位分 500-(3-1-0)=498 num = random.randint(1, max) #1,498 400 s.append((num / 100)) sum = sum + num s.append((amount - sum) / 100) random.shuffle(s) return s
print(hongbao(100, 8))
|