-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathDice_Rolls___10x.m
61 lines (49 loc) · 1.97 KB
/
Dice_Rolls___10x.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
animateFrames();
function animateFrames()
animFilename = 'Dice_Rolls___10x.gif'; % Output file name
firstFrame = true;
framesPerSecond = 24;
delayTime = 1/framesPerSecond;
% Create the gif
for frame = 1:48
drawframe(frame);
fig = gcf();
fig.Units = 'pixels';
fig.Position(3:4) = [300,300];
im = getframe(fig);
[A,map] = rgb2ind(im.cdata,256);
if firstFrame
firstFrame = false;
imwrite(A,map,animFilename, 'LoopCount', Inf, 'DelayTime', delayTime);
else
imwrite(A,map,animFilename, 'WriteMode', 'append', 'DelayTime', delayTime);
end
end
end
function drawframe(f)
% Visualization of the law of large numbers showing how the
% experimental distribution of dice rolls approaches the expected
% distribution as number of rolls increases
close(gcf)
% Internal variables
n=10; % number of dice at each throw
f=n*f; % total number of dice thrown af each round/frame
s=6; % number of sides on dice
% Seed random generator and roll dice
rng(1);
rolls=randi([1,s],1,f); % vector of f dice rolls
counts=histcounts(rolls,1:s+1);
probs=counts/f; % probabilities/distributions of each roll value, expected to converge to 1/s (1/6)
% Top segment of figure for histogram of dice roll frequencies
subplot(4,1,1:3)
bar(probs)
annotation('line',[.136 .9],[.56 .56],Color='r',LineStyle='--',LineWidth=2)
annotation('textbox',[.14 .4 .5 .5],'String','Expected probability is: 1/6 or 0.1667...',Color='r',FitBoxToText='on',LineStyle='none',FontSize=8)
ylim([0 0.4])
title(['Dice Roll Simulation - ',num2str(f),' Rolls'])
% Bottom segment of figure for visualizing dice rolls
subplot(4,1,4)
text(0.5,0.7,'Current Round: ','FontSize',12,'HorizontalAlignment','center')
text(0.5,0.25,char(9855+rolls(end-(n-1):end)),'FontSize',24,'HorizontalAlignment','center')
axis off
end