ValueError: Sample larger than population or is negative
的原因以及解决办法
1.出现
这几天重构kook-valorant-bot
的代码的时候,遇到了这个问题
Job "vip_roll_task (trigger: interval[0:01:20], next run at: 2023-01-25 19:59:21 CST)" raised an exception
Traceback (most recent call last):
File "/home/muxue/.local/lib/python3.10/site-packages/apscheduler/executors/base_py3.py", line 30, in run_coroutine_job
retval = await job.func(*job.args, **job.kwargs)
File "/home/muxue/kook/val-bot/code/main.py", line 793, in vip_roll_task
ran = random.sample(range(0, len(VipRollDcit[msg_id]['user']) - 1), vnum)
File "/usr/local/lib/python3.10/random.py", line 482, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative
error raised during task
Traceback (most recent call last):
File "/home/muxue/.local/lib/python3.10/site-packages/apscheduler/executors/base_py3.py", line 30, in run_coroutine_job
retval = await job.func(*job.args, **job.kwargs)
File "/home/muxue/kook/val-bot/code/main.py", line 793, in vip_roll_task
ran = random.sample(range(0, len(VipRollDcit[msg_id]['user']) - 1), vnum)
File "/usr/local/lib/python3.10/random.py", line 482, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative
这部分代码是用来生成抽奖结果的随机数的,在网上百度了报错之后,得知是random
生成随机数时产生的报错
2.复现
找到对应的代码
ran = random.sample(range(0, len(VipRollDcit[msg_id]['user'])-1), vnum) # 生成n个随机数
这个代码的作用是,生成vnum
个从0到len(VipRollDcit[msg_id]['user']) - 1
的随机数,不包含len(VipRollDcit[msg_id]['user'])-1
咳咳,这里又发现了另外一个bug:我以为是包含右边界的,所以手动
标签:random,Python,py,negative,len,sample,range,larger,msg From: https://blog.51cto.com/musnow/11908614-1
了;现在导致最后一个参加抽奖的用户永远都抽不到奖了