如何获得变量输出的平均值?

时间:2018-11-03 23:49:03

标签: python python-3.x random dice

因此,我有一个输出0或1的变量。现在,我要运行10,000次并获得其平均值。

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的机会是什么。

2 个答案:

答案 0 :(得分:1)

您可以使用random.choices()两次创建10000个6面骰子,-zip()  它们,每个sum() tuple,然后将其送入collections.Counter 数一下请参阅骰子示例,以获取有关代码解释的代码注释。


掷硬币2枚硬币和总价值的掷硬币(0,1)示例:

from collections import Counter
import random

random.seed(42)
r = range(2)   
c = Counter( (map(sum, ( zip(random.choices(r,k=10000),random.choices(r,k=10000))))))
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}%")

输出:

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%

数学:

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% 

0可获得25%,2可获得25%,1可获得50%。


6面骰子示例,将2个骰子的10k滚动并求和:

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%

Doku:

格式化:format mini language以对齐输出中的数字({k:>2}{v:>5}{v / sumall * 100:2.2f}

答案 1 :(得分:0)

好吧,要执行10,000次滚动,您可以这样使用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”的变量。我不确定这是否会干扰循环,因此如果遇到任何问题,请尝试重命名该变量。

相关问题