Python 如何获得变量输出的平均值?
所以,我有一个变量,输出0或1。现在,我想运行10000次,得到平均值Python 如何获得变量输出的平均值?,python,python-3.x,random,dice,Python,Python 3.x,Random,Dice,所以,我有一个变量,输出0或1。现在,我想运行10000次,得到平均值 import random def roll_dice(): available = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6] x = random.sample(available, 1) del available[x[0]] y = random.sample(available, 1) z = x[0] + y[0] if z =
import random
def roll_dice():
available = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]
x = random.sample(available, 1)
del available[x[0]]
y = random.sample(available, 1)
z = x[0] + y[0]
if z == 7:
count = 1
else:
count = 0
print(z)
print(count)
return count
roll_dice()
所以基本上,我想知道掷骰子返回7的几率是多少 要执行10000次滚动,可以使用for循环:
for count in range(0,10001):
# random script here
找到平均值的一种方法是在所述循环中包含if语句,例如:
avg = 0
if z == 7:
count = 1
if count == 1:
avg += 1
else:
count = 0
avg = (avg // 10000)
return avg
希望有帮助
编辑:刚刚意识到您还有一个名为“count”的变量。我不确定这是否会干扰循环,因此如果遇到任何问题,请尝试重命名变量。您可以使用创建10000个六面骰子两次,-
他们,每个人,把它喂进
数一数。请参见骰子示例,以获取对代码进行了一些解释的代码注释
硬币投掷(0,1)2枚硬币的10k投掷示例和总价值: 输出:
Chance for 1: 4989 out of 10000 = 49.89% # about 50%
Chance for 2: 2540 out of 10000 = 25.40% # about 25%
Chance for 0: 2471 out of 10000 = 24.71% # about 25%
数学:
0可以获得25%,2可以获得25%,1可以获得50%
6面骰子示例,共10k,共2个骰子,滚动并求和: 输出:
Counter({ 7: 1673, 8: 1406, 6: 1372, 5: 1090, 9: 1089, 10: 823, 4: 821,
11: 591, 3: 570, 2: 291, 12: 274})
Chance for 7: 1673 out of 10000 = 16.73% # thats about the % of your dice/binary logic
Chance for 8: 1406 out of 10000 = 14.06%
Chance for 6: 1372 out of 10000 = 13.72%
Chance for 5: 1090 out of 10000 = 10.90%
Chance for 9: 1089 out of 10000 = 10.89%
Chance for 10: 823 out of 10000 = 8.23%
Chance for 4: 821 out of 10000 = 8.21%
Chance for 11: 591 out of 10000 = 5.91%
Chance for 3: 570 out of 10000 = 5.70%
Chance for 2: 291 out of 10000 = 2.91%
Chance for 12: 274 out of 10000 = 2.74%
Doku:
格式化:要对齐输出中的数字(
{k:>2}
,{v:>5}
,{v/sumall*100:2.2f}
)整个过程看起来如何?for循环将从roll_dice函数的开头开始,一直运行到count=0的位置。如果你使用我的代码,平均值=。。。和return语句应该在for循环之外。然而,Patrick比我有更多的经验,而且可能有比我更好的解决方案。只是一个提示。为什么不x,y=random。示例(可用,2)
?“示例”确保不会绘制两次相同的示例。或者直接使用z=sum(random.sample(可用,2))
?您的骰子掷骰返回0或1,返回7的几率为零,您不需要任何平均值即可知道这一点。
A B # for summed values:
0 0 25%
1 0 25% # combine it with the one below
0 1 25% # combine it with the one above
1 1 25%
from collections import Counter
import random
random.seed(42)
r = range(1,7)
c = Counter( (map(sum, ( zip(random.choices(r,k=10000),random.choices(r,k=10000))))))
# explanation of the last code line:
# random.choices(r,k=10000) creates 10000 random numbers between 1 and 6
# [1,2,4,...] and [6,1,6,...]
# zip takes 2 such 10k lists and makes 10k tuples
# [ (1,6),(2,1),(4,6) ... ]
# map( sum, zip( ...) ) applies sum() to all 2-tuples
# [7,3,10,...]
# Counter creates a dict with the sum als key and counts how often it occures
# the rest is just pretty printing:
print(c)
sumall = sum(c.values())
for k,v in c.most_common():
print(f"Chance for {k:>2}: {v:>5} out of {sumall} = {v / sumall * 100:2.2f}%")
Counter({ 7: 1673, 8: 1406, 6: 1372, 5: 1090, 9: 1089, 10: 823, 4: 821,
11: 591, 3: 570, 2: 291, 12: 274})
Chance for 7: 1673 out of 10000 = 16.73% # thats about the % of your dice/binary logic
Chance for 8: 1406 out of 10000 = 14.06%
Chance for 6: 1372 out of 10000 = 13.72%
Chance for 5: 1090 out of 10000 = 10.90%
Chance for 9: 1089 out of 10000 = 10.89%
Chance for 10: 823 out of 10000 = 8.23%
Chance for 4: 821 out of 10000 = 8.21%
Chance for 11: 591 out of 10000 = 5.91%
Chance for 3: 570 out of 10000 = 5.70%
Chance for 2: 291 out of 10000 = 2.91%
Chance for 12: 274 out of 10000 = 2.74%