pythonrandomaveragedice

I'm trying to figure out the average, the average of the average, of a series of d6 rolls' value, and total


I'm trying to figure out the average, the average of the average, of a series of d6 rolls' value, total of a series of d6 rolls, and average total of a series of d6 rolls. I keep getting numbers slightly off, and the variable I have ore both confusing me and I think some are redundant. If someone could help me clean up my code & stuff that would be helpful. I expanded upon Stefan B's code,

```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)
```

and added some of my own code, while not making it two dice rolls - my code:

```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)
```

I want to do kind of what it does in there, with the multiplying repeat and numRolls variables, and figure out the total and average of one of the small sets of rolls, and the average of both all rolls and the average of the totals.

I tried to use the letter variable to make better averages, but failed. I think I have redundant stuff, and I can't figure out how to measure the number of rolls and 'sets of rolls' for totals. Help please?


Solution

  • I recommend using more verbose variable names as I think that will help you keep track of what is going on. Conceptually, you want to keep a running total for all rolls of the dice.

    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_index 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_index + 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}")
    

    This will give you a result like:

    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