本文介绍了关于Python的random.choices的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我一直在尝试使用python并尝试一些东西,目前我想知道为什么会出现这种情况随机选择 random.choices([0,1,5,100 ],weight = [0,0,0,0]) 它总是选择列表中的最后一项(100)。我知道在现实中如果所有人都没有可能性,那么你就得不到任何东西,但在这里它专门挑选最后一个,我想知道为什么,任何人都可以解释这个。 我尝试了什么: 尝试查看随机代码,但我真的不明白对不起我目前是业余解决方案 它没有:首先是因为它不是 random.choices 它是 random.choice ,其次是因为统计数据: 导入随机打印(random.choice([0,1,2,3])) print(random.choice([0 ,1,2,3])) print(random.choice([0,1,2,3])) print(random.choice([0,1,2,3]) ) print(random.choice([0,1,2,3])) print(random.choice([0,1,2,3])) print(随机) .choice([0,1,2,3])) print(random.choice([0,1,2,3])) print(random.choice([0,1, 2,3]))打印(random.choice([0,1,2,3]))给我 1 0 3 1 1 1 1 3 2 2 然后这个: 2 0 2 0 0 2 1 0 2 1 请注意,第一个测试的运行为1 - 样本越小,运行的可能性越大:只有四个元素可供选择,很有可能你会连续几次获得相同的值。运行一千次,计数会均匀,但在短期内你应该期待奇怪的结果。 I have been experimenting with python and trying out things and currently i was wondering why this happens in random choicesrandom.choices([0,1,5,100], weights=[0,0,0,0])it always chooses the last item in th list (100). I know in reality if all have zero possibilities then you don't get anything but in here it picks the last one specifically and i was wondering why and can anyone explain this.What I have tried:tried looking at the random code but i can't really understand it sorry im currently an amateur 解决方案 这篇关于关于Python的random.choices的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 05:45