![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/215058/image.png)
MATLAB Help - Rolling Dice Simulation / Central Limit Theorem
조회 수: 2 (최근 30일)
이전 댓글 표시
I need to write a program which throw dices for 100 times and plot mean value/frequency of same numbers. For 1, 2, 3, 4, 5 and 6 dices. I have started with this code:
arr1=[100;1];
arr2=[100;1];
arr3=[100;1];
arr4=[100;1];
arr5=[100;1];
arr6=[100;1];
for n=1:101
a1=randi(6);
a2=randi(6);
a3=randi(6);
a4=randi(6);
a5=randi(6);
a6=randi(6);
single=a1;
duo=a1+a2;
triple=a1+a2+a3;
quadro=a1+a2+a3+a4;
penta=a1+a2+a3+a4+a5;
hexa=a1+a2+a3+a4+a5+a6;
arr1(n)=single;
arr2(n)=duo;
arr3(n)=triple;
arr4(n)=quadro;
arr5(n)=penta;
arr6(n)=hexa;
end
I need to collect all these 100 values in a cell and plot frequency of same numbers. Like this:![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/215052/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/215052/image.jpeg)
댓글 수: 0
채택된 답변
David Wilson
2019년 4월 20일
편집: David Wilson
2019년 4월 20일
A quick hack using randi and cumsum is
N = 100
x = randi(6,N,6);
cs = cumsum(x')';
for i=1:6
subplot(3,2,i)
histogram(cs(:,i))
end
which delivers a plot (not quite in the same order as your one above). Of course with only 100 samples, the approximations are pretty crude.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/215058/image.png)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!