关于Python的random.choices的问题 [英] Question about Python's random.choices

查看:447
本文介绍了关于Python的random.choices的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用python并尝试一些东西,目前我想知道为什么会出现这种情况随机选择



random.choices([0,1,5,100 ],weight = [0,0,0,0])



它总是选择列表中的最后一项(100)。我知道在现实中如果所有人都没有可能性,那么你就得不到任何东西,但在这里它专门挑选最后一个,我想知道为什么,任何人都可以解释这个。



我尝试了什么:



尝试查看随机代码,但我真的不明白对不起我目前是业余

解决方案

我刚试过这个:

  for  i 范围内( 10 ):
random.choices([ 0 1 5 100 ])



得到以下结果:

 [5 ] 
[0]
[0]
[0]
[100]
[5]
[100]
[100]
[0]
[5]



似乎使用全零的权重会影响结果。


< BLO ckquote>它没有:首先是因为它不是 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 choices

random.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

解决方案

I just tried this:

for i in range(10):
	random.choices([0,1,5,100])


and got the following result:

[5]
[0]
[0]
[0]
[100]
[5]
[100]
[100]
[0]
[5]


It seems that using weights of all zeroes affects the outcome.


It doesn't: firstly because it's not random.choices it's random.choice, and secondly because of statistics:

import random
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(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]))

Gives me

1
0
3
1
1
1
1
3
2
2

Then this:

2
0
2
0
0
2
1
0
2
1

Notice that the first test has a "run" of '1's - the smaller your sample, the more likely a "run" is: with only four elements to choose from, it's quite possible that you will get the same value several times in a row. Run it a thousand times, and the count's will even out, but in the short term you should expect "odd looking" results.


这篇关于关于Python的random.choices的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆