MATLAB Answers

How can I increase the interval growth rate?

3 views (last 30 days)
I am working on a code to measure the probability of dice rolls landing on a specific value. For example, if one rolls 3 different dice the probability of the sum of the values being somewhere between 6-12 is much greater than all three dice landing on 1 resulting in a sum of three. At only 10 experiments, there is much error and there is basically a 1 in 10 chance it could land on any number from 3-18. With a much greater number of experiments, the histogram will show a greater value for the sums that were much more common. Here is my problem; I would like to be able to run a for loop that could create four different histograms that would increase in the number of rolls per histogram. For example, the first histogram would include 10 rolls, the next 100, the following 1000, and the last 10000.
What I would like to do is basically say:
N=10
for i:Ni:10000
h=histogram(SumRoll, Bins, 'Normalization','probability');
ylabel('Percent Probability (%=1/100)')
xlabel('Dice Sum Value')
title(['Dice Probability for ' num2str(i) ' Rolls of 3 Dice'])
end
In my head, the way this works out is it starts with 10 rolls, then runs the code again but now with 10*10 rolls, which would be 100, then the following would be 100*10 which would be 1000, finally stopping on 10000 runs. I notice two problems when doing this. The first is it does not save every version of the histogram, the second is the final run of the histogram does not equal 10000 runs, but rather just under it.
How could I alter this code to not only keep each iteration of histograms, but also ensure each histogram stays on the value intended?
MATLAB Version: 9.3.0.713579 (R2017b)

  0 Comments

Sign in to comment.

Accepted Answer

Sambit Senapati
Sambit Senapati on 10 Sep 2018
Instead of using a for loop, you can use a while loop as follows
N=10;
while N<=10000
% your code
N=N*10;
end
Also you are reassigning 'h' with the current histogram in each iteration. Hence, after the loop ends you will only get the histogram from last iteration. If you want to just visualize each histogram add just one line of code as following inside the loop:
while N<10000
figure %%this will create a new figure in each iteration
h=histogram(SumRoll, Bins, 'Normalization','probability');
ylabel('Percent Probability (%=1/100)')
xlabel('Dice Sum Value')
title(['Dice Probability for ' num2str(i) ' Rolls of 3 Dice'])
N=N*10;
end

  1 Comment

Keith Miller
Keith Miller on 14 Sep 2018
Thank you so much! You fixed everything!

Sign in to comment.

More Answers (0)

Sign in to answer this question.