Considering an event where you roll multiple dice and take the max value observed. I am writing a function that will randomly simulate this event multiple times and return the maximum value obtained in each trial.
Not really sure what I have done is correct. Also, I would like to plot a histogram of resulting max values but plt.hist dosen't seem right. Any insights will be appreciated
import random
def multipleRolls(numberoftrials, numberofdice):
trial_list=[]
min = 1
max = 6
for i in range (1,numberoftrials+1):
m=0
for j in range(1,numberofdice+1):
k = random.randint(min, max)
if m<k:
m=k
trial_list.append(m)
plt.hist(trial_list)
plt.show()
print(trial_list)
multipleRolls(3,5)
As a proof to see the correct number of die are being rolled and the maximum is selected you could create a list of all rolled die and print the answer, then pass the result to a normal plt.hist, example:
import random
def multipleRolls(numberoftrials, numberofdice):
trial_list=[]
alltrials=[]
min = 1
max = 6
for i in range (1,numberoftrials+1):
singletrial=[]
m=0
for j in range(1,numberofdice+1):
k = random.randint(min, max)
if m<k:
m=k
singletrial.append(k)
alltrials.append(singletrial)
trial_list.append(m)
return trial_list, alltrials
trial_list, alltrials = multipleRolls(3,5)
print(trial_list, alltrials)
import matplotlib.pyplot as plt
plt.hist(trial_list)
plt.show()
Example response:
[5, 6, 6] [[1, 1, 5, 1, 5], [5, 2, 6, 2, 4], [5, 2, 6, 4, 6]]