【问题标题】:I'm trying to figure out the average, the average of the average, of a series of d6 rolls' value, and total我正在尝试计算一系列 d6 rolls 值的平均值,平均值的平均值,以及总值
【发布时间】:2023-01-27 02:03:13
【问题描述】:

我试图计算一系列 d6 卷值的平均值、平均值的平均值、一系列 d6 卷的总和以及一系列 d6 卷的平均总数。我的数字总是略有偏差,而且我所拥有的变量都让我感到困惑,而且我认为有些是多余的。如果有人可以帮助我清理我的代码和有用的东西。我扩展了 Stefan B 的代码,

```python

from random import randint

numRolls = int(input("How many times will you roll the dice? "))
s = 0

for roll in range(numRolls):  
        d1 = randint(1, 6)
        d2 = randint(1, 6)
        s += d1 + d2
        print("You rolled a", d1, "and a", d2)

print("Your total for all of the rolls is", s)
print("Your average for all of the rolls is", s / numRolls)
```

并添加了一些我自己的代码,而不是掷两次骰子——我的代码:

```python
from random import randint

k = 0
x = 0
z = 0
v = 0
a = 0
j = 0
repeat = int(input("How many times would you like to repeat the following command? "))
numRolls = int(input("How many times will you roll the dice? "))
for roll in range(repeat):

  s = 0
  b = 0
  for roll in range(numRolls):
    d6 = randint(1, 6)
    a += d6
    s += d6
    v = v + 1
    y = s / numRolls
    z += y
    k += z
  j = j + 1
  print("Your total for all of the rolls is", s)
  print("Your average for all of the rolls is ", s / numRolls)
rollsets = numRolls / 6
print("Your total average for the total of all rolls is ", z / v)
print("Your total average for all of the rolls is ", a / v)
#print(y)
#print(x)
#print(v)
#print(z)
#print(k)
```

我想做它在那里做的事情,乘以 repeat 和 numRolls 变量,并计算出一小组卷的总数和平均值,以及所有卷的平均值和总数的平均值。

我试图使用字母变量来获得更好的平均值,但失败了。我想我有多余的东西,而且我无法弄清楚如何衡量总数的卷数和“卷集”。请帮忙?

【问题讨论】:

  • 请标记一种语言。如果这是 python,请注意正确的缩进是必不可少的。阅读帮助以了解如何正确格式化代码。

标签: python random average dice


【解决方案1】:

我建议使用更详细的变量名,因为我认为这将帮助您跟踪正在发生的事情。从概念上讲,您希望保留所有掷骰子的总计。

from random import randint

number_of_dice = int(input("How many dice will you roll? "))
number_of_rounds = int(input("How many rounds will you roll the dice? "))

sum_all_rolls = 0
sum_all_round_averages = 0

print(f"Rolling {number_of_dice} dice, {number_of_rounds} times")
for round in range(number_of_rounds):
    rolls = [randint(1, 6) for _ in range(number_of_dice)]
    sum_this_round = sum(rolls)
    average_this_round = sum_this_round / number_of_dice
    print(f"Round {round + 1}. You rolled {rolls} and for a total of {sum_this_round} average {average_this_round}.")

    sum_all_rolls += sum_this_round
    sum_all_round_averages += average_this_round

print(f"Your total for all of the rolls is: {sum_all_rolls}")
print(f"Your average for all of the rolls is: {sum_all_rolls / (number_of_rounds * number_of_dice)}")

print(f"Your total for all round averages is: {sum_all_round_averages}")
print(f"Your average for all round averages is: {sum_all_round_averages/ number_of_rounds}")

这会给你这样的结果:

How many dice will you roll? 3
How many rounds will you roll the dice? 6
Rolling 3 dice, 6 times
Round 1. You rolled [3, 4, 4] and for a total of 11 average 3.6666666666666665.
Round 2. You rolled [6, 6, 1] and for a total of 13 average 4.333333333333333.
Round 3. You rolled [4, 3, 2] and for a total of 9 average 3.0.
Round 4. You rolled [3, 2, 1] and for a total of 6 average 2.0.
Round 5. You rolled [4, 5, 6] and for a total of 15 average 5.0.
Round 6. You rolled [5, 6, 3] and for a total of 14 average 4.666666666666667.
Your total for all of the rolls is: 68
Your average for all of the rolls is: 3.7777777777777777
Your total for all round averages is: 22.666666666666668
Your average for all round averages is: 3.777777777777778

【讨论】:

  • 我的意思是我重新使用 Stefan B 的代码来做我自己的代码,但我改变了一些目标。我不想要两个 d6,而是一个 d6 在 y 组中每​​组滚动 x 次,目的是了解每组的平均滚动、每组的总数、所有滚动的总体平均值以及平均值全部的。
  • @ObAnon 我更新了我认为您想要做的事情的答案。
猜你喜欢
  • 1970-01-01
  • 2012-06-19
  • 2017-02-06
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 2022-11-03
相关资源
最近更新 更多