我在MatLab中获得了创建程序的任务:
我已经想出如何做第一部分:
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');
我对如何实现第二部分感到磕磕绊绊,因为我不确定如何从直方图中获取最大值和最小值。这是否可能,或者我是否必须采取另一种方式?我很失落。任何帮助都会很棒。
答案 0 :(得分:1)
您可以修改现有代码,将直方图值分配给变量,并使用它来查找百分比差异。
histValues = hist(sumOfRoll, Bins);
这里,histValues
保存每个bin的直方图值。然后,您可以使用这些值来计算差异和百分比差异。
diffInOutcomes = (max(histValues) - min(histValues))
percentDiff = (diffInOutcomes)*100/numberOfRolls