216k views
3 votes
Write a Python programs to do frequency counting of the sum of two dice. Your function should accept an integer n parameter to run n trials (i.e., multiple rollings of the two dice); You can use the Python function randrange(1, 7) to simulate the roll of one die; After running the n trials, your function should return the sum of two dice that occurs most frequently.

User Janelly
by
5.5k points

1 Answer

6 votes

Answer:

Step-by-step explanation:

The following program uses Numpy import to detect the frequency of all the elements in an array and ouput the one that appears the most. The function creates random dice rolls and adds them to the array, looping the number of times that the trials parameter states. Then finally printing out the original array and the sum that appeared the most frequently.

from random import randrange

import numpy as np

def highestFrequency(trials):

products = []

for x in range(trials):

product = randrange(1, 7) * randrange(1, 7)

products.append(product)

x = np.array(products)

print("Original Array: " + str(list(products)))

print("Most frequent value in the above array:")

print(np.bincount(x).argmax())

highestFrequency(7)

Write a Python programs to do frequency counting of the sum of two dice. Your function-example-1
User Moe Tsao
by
5.8k points