0
votes

I created a dice function but i don't know how to create a histogram for it. Each bin in the histogram should represent a possible value of for the sum of the dice. For example, for NumDice = 1, the bins should span the values 1 to 6. For NumDice = 2 the bins should range from 2 to 12. thanks!

function SumDice=RollDice(NumDice,NumRolls)
NumDice=1
NumRolls=500
distribution=zeros(NumDice*6,1);
for roll=1:NumRolls
  diceValues = randi(6,[NumDice 1]);
  totaldiceValue = sum(diceValues);
        distribution(totaldiceValue) = distribution(totaldiceValue) +1;
end
end
1
Is this some homework question going around at the moment? I just answered two questions on exactly this topic! See here and here for some suggestions on improving your code. ps, I'm downvoting your question as you clearly did NOT bother to check questions currently on SO before posting. - Colin T Bowers
@ColinTBowers I noticed this too. Someone must have sent the entire class here for help. - jerad

1 Answers

0
votes

You can use the bar function to plot a bar graph when you already have the values of each bar.

valueMin = NumDice;
valueMax = 6 * NumDice;

bar(valueMin:valueMax, distribution(valueMin:valueMax), "hist")