I need to generate a probability histogram of the number of rolls before a sum of 7 occurs while rolling two dice. The experiment works properly and through 10,000 iterations I get data that looks as you would expect. I am having lots of trouble in displaying this data in a histogram however. The problem is that there is a large amount of extra data that seems to be printed onto the histogram that is not present in the vector that I've passed to hist(). This shows up as a large amount of infinitely large bins at large values on the x-axis.
Since the probability of rolling a sum of 7 is 6/36 = 1/6, it is typical for this to occur on one of the first few rolls. Here i have a row vector "rollbins", where the ith entry holds the frequency of the experiment requiring "i" rolls. After many iterations of the experiment, rollbins has its first few elements large with each subsequent entry smaller until the 45th is usually zero.
I've used the hist() function with a bins vector argument and per this question I've used xlim() to limit the display to only 0-45 on the x-axis. However the output is not limited with or without xlim().
Any help is greatly appreciated :)
iters = 1000;
% do not consider extreme results
maxrolls = 45;
% rollbins(i) is how many experiments occured with i rolls
rollbins = zeros(1, maxrolls);
for r=1 : 1 : iters
% roll die until get sum of 7, note times taken
sum = 0;
% the amount of rolls the experiment takes
rolls = 0;
while sum ~= 7
rolls = rolls + 1;
% sum two rolls of a die (same as one roll two dies)
sum = floor( 6*rand(1) + 1 ) + floor( 6*rand(1) + 1 );
end
% assign if within the vector's limits; discards outliers
if rolls < maxrolls
rollbins(rolls) = rollbins(rolls) + 1;
end
end
% 1,2,3...45
range = 1:1:maxrolls;
% limit the values on x-axis to 0-45
xlim([0 maxrolls]);
% the histogram shows more than 45 vertical bars
hist(rollbins, range)
edit: the xlim() call should come after the hist() function. Leaving the semi-colon off of the last graphics function (ylim) enables these effects to take place.
hist(rollbins, range);
xlim([0 maxrolls-1]);
ylim([0 iters / 5])
However I now realize that the bars are much too short still and the bins appear in intervals of .1 not 1 as I'd expected.

sumandrangeas variable names, it will only cause headache in the future... - Amro