# Write a python program:Let the user specify S (the number of sides on a die) and N(the number of tosses of two S -sided dice).Assume ((S << N) and (3 <= S <= 20)). Since each die side is labeled with a unique integer taken from the range[ 1,S ], the sum of each dice toss is in the range [ (2=1+1),(2*S=S+S) ]. For example, whenS = 6 , then the sum of each dice toss is in the range [ 2,12 ]. MakeN dice tosses and count how frequently each sum occurs. Output ahorizontal histogram that depicts the frequency distribution of the sums of theN dice tosses. Every sum in the range [ 2,2*S ] and its frequency of occurrence should be output using the formatXXX (XXXXXXXX) | ******whereXXXis the sum,(XXXXXXXX)is the frequency, and each'*'represents 1% of the 100% of frequencydistribution. The number of '*' output for each sum is computed by rounding the quotient (counts[sum]/N) up to the nearest integer. The entire histogram will contain (2*S-2+1) lines2 (XXXXXXXX) | ******3 (XXXXXXXX) | ******. . .. . .2*S (XXXXXXXX) | ******

Question
1 views

Write a python program:

Let the user specify S (the number of sides on a die) and N
(the number of tosses of two S -sided dice).
Assume ((S << N) and (3 <= S <= 20)). Since each die side is labeled with a unique integer taken from the range
[ 1,S ], the sum of each dice toss is in the range [ (2=1+1),(2*S=S+S) ]
. For example, when
S = 6 , then the sum of each dice toss is in the range [ 2,12 ]. Make
N dice tosses and count how frequently each sum occurs. Output a
horizontal histogram that depicts the frequency distribution of the sums of the
N dice tosses. Every sum in the range [ 2,2*S ] and its frequency of occurrence should be output using the format
XXX (XXXXXXXX) | ******
where
XXX
is the sum,
(XXXXXXXX)
is the frequency, and each
'*'
represents 1% of the 100% of frequency
distribution. The number of '*' output for each sum is computed by rounding the quotient (counts[sum]/N) up to the nearest integer. The entire histogram will contain (2*S-2+1) lines
2 (XXXXXXXX) | ******
3 (XXXXXXXX) | ******
. . .
. . .
2*S (XXXXXXXX) | ******
check_circle

star
star
star
star
star
1 Rating
Program Instructions:
• Import the random library and prompt the user for the values of N and S.
• Use a for loop for N times to generate random numbers between 2 to 2*S and store them in a list.
• Use a for loop and dictionary to count the frequency of each number/sum.
• Use a for loop and sorted() function to print the dictionary values in sorted manner.
• Use round() function to round the quotient up to the nearest integer.
• The value of sum is multiplied by 100 before diving it with N to increase the number otherwise the result of round() function will be 0.
Program:

# import random to generate random numbers

import random

# prompt user for S

S = int(input('Enter the value of S (the number of sides on a die): '))

# prompt user for N

N = int(input('Enter the value of N (the number of tosses of two S-sided dice): '))

dice_sum = []

# generate sums in range 2 to 2*S for N tosses

for i in range(N):

dice_sum.append(random.randint(2,2*S))

freq = {}

# use a dictinory to calculate the count of each number

for items in dice_sum:

freq[items] = dice_sum.count(it...

### Want to see the full answer?

See Solution

#### Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in