• Remix
  • Share
  • New Entry

  • Josh

  • /
  • Law of Large Numbers & Dice Rolls

on 27 Nov 2023
  • 15
  • 15
  • 1
  • 1
  • 596
drawframe(10);
Write your drawframe function below
function drawframe(f)
close(gcf)
% Internal variables
n=1; % 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 .905],[.415 .415],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 1])
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.2,char(9855+rolls(end-(n-1):end)),'FontSize',24,'HorizontalAlignment','center')
axis off
end
Animation
Remix Tree