Reputation: 257
I have been given the task in MatLab of creating a program that:
I have already figured out how to do the first part:
numberOfDice = 2; %Number of dice to be rolled
numberOfDots = 6; %Number of max. dots allowed on a die face
numberOfRolls = 100000; %Number of times the die(s) are rolled
%#Generate the rolls and obtain the sum of the rolls
AllRoll = randi(numberOfDots, numberOfRolls, numberOfDice);
sumOfRoll = sum(AllRoll, 2);
%#Determine the bins for the histogram
Bins = (numberOfDice:numberOfDots * numberOfDice)';
%#Build the histogram
hist(sumOfRoll, Bins);
title(sprintf('The Frequency of Different Sums from %d %d-sided Dice after %d Rolls', numberOfDice, numberOfDots, numberOfRolls));
xlabel(sprintf('The Sum of %d Dice', numberOfDice));
ylabel('Count');
I'm stumbling on how to achieve the 2nd part, because I'm uncertain how to obtain the maximum and minimum values from my histogram. Is this even possible, or do I have to go about it another way? I'm quite lost. Any help would be amazing.
Upvotes: 0
Views: 1023
Reputation: 202
You can just modify your existing code to assign the histogram values to a variable and use it to find the percentage difference.
histValues = hist(sumOfRoll, Bins);
Here, histValues
holds the values of histogram for each bin. Then, you can use these values to figure out the difference and percentage difference.
diffInOutcomes = (max(histValues) - min(histValues))
percentDiff = (diffInOutcomes)*100/numberOfRolls
Upvotes: 1