網站首頁 編程語言 正文
功能描述
抽獎系統包含如下特點:
1、可給不同抽獎者設置不同的權重
2、先從價值高的獎品開始抽
3、已經中獎的人,不再參與后續的抽獎
代碼
第一個數字表示某一個用戶的中獎編號,第二個數字表示該用戶的中獎權重。名單.txt
內容為:
1:20
2:10
3:8
4:6
5:6
6:3
7:2
8:1
main.py
內容為:
# 抽獎工具 # -*- coding:utf-8 -*- from random import choice def read_name_list(txt_name): with open(txt_name, 'r', encoding='utf-8') as f: txt_list = f.readlines() for i in range(len(txt_list)): txt_list[i] = txt_list[i].rstrip('\n') return txt_list def lottery_draw(name_list, prize_box): full_number = [] for name in name_list: number, weight = name.split(':') full_number += [number] * int(weight) for prize in prize_box: luck_number = choice(full_number) print('【{}】的中獎用戶編號為:{}'.format(prize, luck_number)) full_number = [i for i in full_number if i != luck_number] if __name__ == '__main__': # 導入名單 name_list = read_name_list('名單.txt') # 設置獎項 prize_box = ['汽車', '電腦', '杯子', '香蕉', '5毛紅包'] # 抽獎 lottery_draw(name_list, prize_box)
運行結果如下:
【汽車】的中獎用戶編號為:5
【電腦】的中獎用戶編號為:1
【杯子】的中獎用戶編號為:3
【香蕉】的中獎用戶編號為:2
【5毛紅包】的中獎用戶編號為:6
驗證
加一個統計函數statistics
、修改下lottery_draw
的輸入輸出,然后抽獎1000000次,看看結果是否和我們設想的一樣不同用戶中獎權重不同。main.py
完整代碼如下:
# 抽獎工具 # -*- coding:utf-8 -*- from random import choice from collections import Counter def read_name_list(txt_name): with open(txt_name, 'r', encoding='utf-8') as f: txt_list = f.readlines() for i in range(len(txt_list)): txt_list[i] = txt_list[i].rstrip('\n') return txt_list def lottery_draw(name_list, prize_box, res): full_number = [] for name in name_list: number, weight = name.split(':') full_number += [number] * int(weight) for prize in prize_box: luck_number = choice(full_number) full_number = [i for i in full_number if i != luck_number] res.append(prize+':'+luck_number) return res def statistics(res): prize_cal = {'汽車':[], '電腦':[], '杯子':[], '香蕉':[], '5毛紅包':[]} for i in res: prize, luck_number = i.split(':') prize_cal[prize].append(luck_number) for prize, number in prize_cal.items(): print('【{}】的各用戶中獎次數:{}'.format(prize, Counter(number))) if __name__ == '__main__': # 導入名單 name_list = read_name_list('名單.txt') # 設置獎項 prize_box = ['汽車', '電腦', '杯子', '香蕉', '5毛紅包'] # 驗證抽獎系統 res = [] for i in range(1000000): # 抽獎 res = lottery_draw(name_list, prize_box, res) # 統計數據 statistics(res)
結果為:
【汽車】的各用戶中獎次數:Counter({'1': 356978, '2': 178116, '3': 143076, '5': 107189, '4': 107141, '6': 53638, '7': 35943, '8': 17919})
【電腦】的各用戶中獎次數:Counter({'1': 262385, '2': 192168, '3': 160879, '5': 125696, '4': 125308, '6': 65905, '7': 44822, '8': 22837})
【杯子】的各用戶中獎次數:Counter({'2': 189568, '1': 180131, '3': 173171, '4': 144363, '5': 143846, '6': 82818, '7': 56424, '8': 29679})
【香蕉】的各用戶中獎次數:Counter({'3': 173540, '2': 170010, '4': 162484, '5': 162387, '1': 111469, '6': 104839, '7': 75181, '8': 40090})
【5毛紅包】的各用戶中獎次數:Counter({'5': 173954, '4': 173772, '3': 157203, '6': 139308, '2': 136231, '7': 103678, '1': 58633, '8': 57221})
可以看出以下結論:
- 通過第一行可以發現,數字之間出現的比例是和我們設置的權重是一樣。例如
1
出現次數約等于2
的兩倍、4
和5
的出現次數相近且均為6
的兩倍。說明中獎數字出現的概率和預設權重是一樣的。 - 通過后面的行可以發現,對于權重大的數字(例如
1
),即使第一個獎品沒中獎,大概率會在前幾個獎品中獎,所以最后一個獎品里1
出現的次數很少,因為1
基本都是前面出現了。所以權重大的數字優先中價值高的獎品,和設想的一樣。 - 對于權重小的數字,例如
7
、8
,不管是哪一個獎品,出現次數都是最小的,說明這些數字中獎概率低。原因也很簡單,因為中獎數字的個數是小于獎品數量的,所以權重小的數字中獎概率低,很可能5個獎品里都抽不到它們。如果設置為8個獎品的話,那么末尾的獎品7
、8
出現次數就會很高,因為此時中獎數字的個數等于獎品數量,所有數字都100%中獎,那么權重小的數字基本上都是中價值低的小獎。例如下面是8個獎品時的實驗結果:
【汽車】的各用戶中獎次數:Counter({'1': 357293, '2': 178396, '3': 142964, '4': 106969, '5': 106885, '6': 53787, '7': 35860, '8': 17846})
【電腦】的各用戶中獎次數:Counter({'1': 262439, '2': 191894, '3': 160317, '4': 125823, '5': 125372, '6': 66868, '7': 44747, '8': 22540})
【杯子】的各用戶中獎次數:Counter({'2': 189590, '1': 179792, '3': 172228, '4': 144894, '5': 144557, '6': 82114, '7': 57173, '8': 29652})
【香蕉】的各用戶中獎次數:Counter({'3': 173294, '2': 170689, '4': 162112, '5': 162085, '1': 111105, '6': 105137, '7': 75643, '8': 39935})
【5毛紅包】的各用戶中獎次數:Counter({'5': 174098, '4': 173631, '3': 158224, '6': 138613, '2': 135805, '7': 103940, '1': 58826, '8': 56863})
【空氣】的各用戶中獎次數:Counter({'6': 201214, '7': 162491, '4': 156446, '5': 156151, '3': 117426, '8': 95013, '2': 87502, '1': 23757})
【垃圾】的各用戶中獎次數:Counter({'7': 282600, '6': 225701, '8': 187694, '5': 99963, '4': 99165, '3': 60536, '2': 38211, '1': 6130})
【一巴掌】的各用戶中獎次數:Counter({'8': 550457, '7': 237546, '6': 126566, '4': 30960, '5': 30889, '3': 15011, '2': 7913, '1': 658})
原文鏈接:https://blog.csdn.net/weixin_38705903/article/details/128065246
相關推薦
- 2022-11-26 ASP.NET延遲調用或多次調用第三方Web?API服務_實用技巧
- 2022-07-25 C#中Linq的入門教程_C#教程
- 2022-04-09 SpringBoot 提示:RequestRejectedException:The request
- 2022-11-19 項目適?Oracle改造及SSL安全性配置問題匯總詳解_oracle
- 2023-12-02 富文本組件中圖片間空白處理小技巧
- 2022-07-09 JQuery中this的指向詳解_jquery
- 2022-10-29 前端圖片上傳發現圖片倒置解決方案 圖片鏡像效果實現
- 2022-06-16 nginx限流及配置管理實戰記錄_nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支