count number of occurance

5 ビュー (過去 30 日間)
Me
Me 2022 年 8 月 24 日
コメント済み: Me 2022 年 8 月 25 日
Hi, This is my first code in my life and I am somehow stuck.
Rolling two dice 100 times. sum of the two dice for each roll. The sum will be numbers between x= (2,3,4,5,6,7,8,9,10,11,12)
Then find how many occurance of the sum is qual to x.
the output would be like this:
x= (2,3,4,5,6,7,8,9,10,11,12)
y= (5,1,4,8,7,4,2,2,4,8,3)
here is my trial :
r1 = randi([1 6],1,100)
r2 = randi([1 6],1,100)
DiceSum=r1+r2
x = [2 3 4 5 6 7 8 9 10 11 12];
Then I stuck
  3 件のコメント
James Tursa
James Tursa 2022 年 8 月 24 日
What is the exact wording of the assignment?
Bruno Luong
Bruno Luong 2022 年 8 月 24 日
編集済み: Bruno Luong 2022 年 8 月 24 日
"the output would be like this:"
x= (2,3,4,5,6,7,8,9,10,11,12)
y= (5,1,4,8,7,4,2,2,4,8,3)
If y is the occurance count to me the above numbers are very very unlikely. It adds up to 48 ad not 100!
If you tell this issue to your professor, you'll get the best (or the worst) grade.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2022 年 8 月 24 日
Rather than
r1 = randi([1 6],1,100);
r2 = randi([1 6],1,100);
DiceSum=r1+r2;
to get just one roll, I'd use randi to get all 100 rolls in just one call to randi, like this:
numRolls = 100;
numDice = 2;
rollResults = randi([1, 6], numRolls, numDice)
rollResults = 100×2
3 5 5 2 6 5 3 2 4 4 4 5 4 5 5 3 1 1 5 1
DiceSum = sum(rollResults, 2)
DiceSum = 100×1
8 7 11 5 8 9 9 8 2 6
% All done. Now just some fancy plotting stuff.
histogram(DiceSum);
grid on;
caption = sprintf('Results of %d rolls of %d dice', numRolls, numDice);
title(caption);
ylabel('Count')
xlabel('2-Roll Sum')
  1 件のコメント
Me
Me 2022 年 8 月 25 日
thanks alot. That is really help

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by