0

I am trying to simulate dice being rolled. Die_1 + Die_2 five times. The program runs, but the math is wrong. I have tried this multiple ways, but cannot get the math correct. The die are only being rolled one at a time, so a 1 and a six are possibilities. It must be an overly tired oversight. Any ideas would be fantastic. Code and output below. Thank you to anyone who can help.

# This program will simulate dice rolls 5 different
# times and add the total of each roll.
import random
import math
MIN = 1
MAX = 6
ROLLS = 5

def main():
    for count in range(ROLLS):
        die_1 = (random.randint(MIN, MAX))
        die_2 = (random.randint(MIN, MAX))
        combined_roll = point(die_1, die_2)
        print('Here are the combined rolls for the dice!')
        print(random.randint(MIN, MAX))
        print(random.randint(MIN, MAX))
        print('The combined roll is:', combined_roll)

def point(die_1, die_2):
    roll_1 = die_1 + die_2

    combined_roll = roll_1 
    return combined_roll
main()

Here are the combined rolls for the dice!
4
3
The combined roll is: 4
Here are the combined rolls for the dice!
2
2
The combined roll is: 7
Here are the combined rolls for the dice!
5
4
The combined roll is: 5
Here are the combined rolls for the dice!
3
5
The combined roll is: 9
Here are the combined rolls for the dice!
3
1
The combined roll is: 11

3 Answers 3

2

The math and everything is correct. It is indeed a symptom of being tired.

You're printing out entirely new numbers in these 2 lines:

print(random.randint(MIN, MAX))
print(random.randint(MIN, MAX))

Compared to what your die rolls actually were, earlier in time.

die_1 = (random.randint(MIN, MAX))
die_2 = (random.randint(MIN, MAX))

Time has passed so your random number generation is going to be in a different state.

So change the prints to:

print(die_1)
print(die_2)
Sign up to request clarification or add additional context in comments.

1 Comment

@user3758480 EugeneK is right, you are repeating the same error in another question. Having a sleep could be the best way to proceed.
1

This is best achieved with a simple function and random.randint:

>>> from random import randint
>>> def roll_dice(n=1):
...     return [randint(1, 6) for _ in range(n)]
...

1-die roll:

>>> roll_dice()
[2]
>>> roll_dice()
[2]
>>> roll_dice()
[5]

2-die roll:

>>> roll_dice(2)
[6, 2]
>>> roll_dice(2)
[6, 2]
>>> roll_dice(2)
[5, 5]
>>>

You can easily sum a 2-die roll by:

>>> sum(roll_dice(2))
6
>>> sum(roll_dice(2))
7
>>> sum(roll_dice(2))
8

Comments

0

The second set if print statements mean to print die_1 and die_2, nit calls to random again. If you call random again, you get new random numbers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.