Question

In Python, design a program that simulates the roll of a pair of dice (two dice)...

In Python, design a program that simulates the roll of a pair of dice (two dice) and calculates the probability that various number combinations will be rolled. Let the user enter a number. This is the number of times they will roll the dice. You will roll the dice in a loop and keep track of the number of times that various rolls occur. When the loop has completed, your program should produce a table that shows the value of the roll accompanied by the number of times that roll occurred and the percentage of the time that value occurred. The output should look like

total iterations: 100

number rolls percent

1 80 .80

2 10 .10

3 10 .10 ...

We can't use frequency or dict functions

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question, I have used simple list to track the counts and as asked I have not used any dictionary function or frequency function.

Here is the code, Let me know for any help. 

The minimum sum can be when both the values are 1 and so the sum is 2, the max value can be 16 ( both 6)

the getDiceRolls() function generates two random number between 1 and 6 and returns the values as a tuple.

=============================================================================


import random

def getDiceRolls():

    return random.randint(1,6), random.randint(1,6)

def main():
    number_of_rolls = int(input('Enter the number of rolls: '))

    rolls_count = [0]*13
    for _ in range(number_of_rolls):
        dice1,dice2 = getDiceRolls()
        sum_roll_value = dice1+dice2
        rolls_count[sum_roll_value]+=1

    print('Total iterations: {}'.format(number_of_rolls))
    for i in range(2,len(rolls_count)):
        print('{:>2} {:>2} {:4.2f}%'.format(i,rolls_count[i],100*rolls_count[i]/number_of_rolls))
main()

==============================================================

Add a comment
Know the answer?
Add Answer to:
In Python, design a program that simulates the roll of a pair of dice (two dice)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In python 3, Write a program in Python that simulates the roll of two dice. The...

    In python 3, Write a program in Python that simulates the roll of two dice. The first die is a 6-sided die. The second die is an 8-sided die. Generate a random number for a die and store it in variable number1. Generate a random number for a die and store it in variable number2. Note: Submit your code with the print("You rolled two dice:", number1, "and", number2) statement.

  • Create a Dice Game: PYTHON This game is between two people. They will roll a pair...

    Create a Dice Game: PYTHON This game is between two people. They will roll a pair of dice each 10 times. Each roll will be added to a total, and after 10 rolls the player with the highest total will be the Winner! You will use the RANDOM class RANDRANGE to create a random number from 1 to 6. You'll need a function for "rollDice" to roll a pair of dice and return a total of the dice. You must...

  • (MATLAB) Write a program that simulates a dice roll by picking a random number from 1-6...

    (MATLAB) Write a program that simulates a dice roll by picking a random number from 1-6 and then picking a second random number from 1-6.    How many times do you get two 1’s. What many times do you get two 1’s if you simulate 10,000 rolls of a pair of dice?

  • please do it in C++ Write a program that simulates the rolling of two dice. The...

    please do it in C++ Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2and 12...

  • Write a java program that simulates the rolling of four dice. The program should use random...

    Write a java program that simulates the rolling of four dice. The program should use random number generator to roll dice. The sum of the four values should then be calculated. Note that each die can show an integer value from 1 to 6, so the sum of the four values will vary from 4 to 24, with 14 being the most frequent sum and 4 and 24 being the least frequent sums. Your program should roll the dice 12,960...

  • Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In...

    Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In Yahtzee, you roll five dice and then use those dice to make certain combinations. For example, the Yahtzee combination is formed by all five dice having the same value. A large straight occurs when all of the dice are in consecutive order (e.g., 1,2,3,4,5 or 2,3,4,5,6). If you haven’t played Yahtzee, you can find out more from various online sources such as Wikipedia. Once...

  • With the use of python IDLE version 3.7.2, use the while loop that iterates 100,000 times....

    With the use of python IDLE version 3.7.2, use the while loop that iterates 100,000 times. In the loop code, roll a pair of dice by generating(and summing) two random numbers in the range 1 through 6. Report to the user how many data points have been generated (i.e how many times the dice have been rolled), as in: successfully simulated 100,000 dice rolls. Create a histogram of the dice rolls showing how many times a 2 was rolled, how...

  • In the game of Yahtzee®, players roll five dice simultaneously to try to make certain combinations....

    In the game of Yahtzee®, players roll five dice simultaneously to try to make certain combinations. In a single turn, a player may roll up to three total times. On the first roll, the player rolls all five dice. On subsequent rolls, the player may choose to roll any number of the five dice ("keeping" dice by leaving selected unrolled dice on the table). If a player's first roll results in exactly three 5's (e.g., 2-5-5-3-5), what is the probability...

  • (1 point) Using an if Statement In a Loop A common thing to do is to...

    (1 point) Using an if Statement In a Loop A common thing to do is to use variables to keep track of some sort of count. When used in a loop we count things very quickly. Scenario: If you roll a pair of dice, rolling a 12 (two sixes) is rare. How rare? If you were to roll a pair of dice 1,000 times, on average, how many times would it come up as 12? To figure this out, we...

  • using python [6] Craps is a dice-based game played in many casinos. Like blackjack, a player...

    using python [6] Craps is a dice-based game played in many casinos. Like blackjack, a player plays against the house. The game starts with the player throwing a pair of standard, six-sided dice. If the player rolls a total of 7 or 11, the player wins. If the player rolls a total of 2,3, or 12, the player loses. For all other roll values, the player will repeatedly roll the pair of dice until either she rolls the initial value...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT