In: Computer Science
Write the program to analyze whether a die is fair by counting how often the values 1, 2, ..., 6 appear. • The input is a sequence of die toss values. • The output is a table with the frequencies of each die value, as shown in the following figure. o
Here iam providind the code and explanation in comments for the given problem.In this program we generated 10 random numbers as input sequence of die toss values.
Code:-
Code in text format:-
import random # library for random number generation
b = [] # declaration of an array
i = 0
one = two = three = four = five = six =0 #declaration of
variablers
while (i < 10): # taking 10 random number
b.append(random.randint(1,6)) # random in the
range of 1 to 6
#print(b[i])
i = i+1
for i in range(10): #checking the numbers in range of 10
if (b[i] == 1): # if the current die value is
equal to 1 then we increment the one variable
one = one+1
elif(b[i] == 2):
two = two +1
elif(b[i] == 3):
three = three +1
elif(b[i] == 4):
four = four +1
elif(b[i] == 5):
five = four +1
else:
six = six+1
print("The table with the frequencies of each die value") #printing
the frequencies of die values
print("one |" + " Two |" + " Three |" + " Four |" + " Five |" + "
Six |")
print (str(one)+" "+
str(two)+" " +
str(three)+"
"+str(four)+"
"+str(five)+" "+str(six) )
Output:-