How do you write a command for two loops?

1 回表示 (過去 30 日間)
Nick Haufler
Nick Haufler 2015 年 9 月 30 日
コメント済み: Nick Haufler 2015 年 9 月 30 日
In the attached document how would you write a command to generate two random numbers for two dice. Below is for one dice; so do you just change roll=randi statement, and put in 2,12 in place of 1,6?
rng('shuffle')
numRolls=input('Please enter how many rolls you would like to simulate:')
counts = zeros(1,6)
for r = 1 : numRolls
roll = randi([1 6],1)
counts(roll) = counts(roll)+1
end
counts = (counts / numRolls)*100

採用された回答

Image Analyst
Image Analyst 2015 年 9 月 30 日
Try this:
rng('shuffle')
numRolls=input('Please enter how many rolls you would like to simulate:')
counts = zeros(1, 12);
numDice = 2;
for r = 1 : numRolls
roll = randi([1 6],1,numDice);
theSum = sum(roll);
counts(theSum) = counts(theSum) + 1;
end
counts = (counts / numRolls) * 100
  1 件のコメント
Nick Haufler
Nick Haufler 2015 年 9 月 30 日
Thanks, you're a really big help. Appreciate it.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by