To roll a dice simulator
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I'm trying to create a dice simulator using MATLAB. I have to use the function input and output to create a loop. We let K be the integer from 1 to 6 that represents the number we want to see from the die. We let n be the integer that represent the number of rolls for the first K to appear from the die. We can use the randi function.
This is what I have so far:
numberOfExperiments = 6; %I know that is our n or the number of rolls%
numberOfDice = 1;
rollValues = randi(6, [numberOfDice, numberOfExperiments])
I'm trying to connect that with this:
function [n] = firstRollK(K)
% code
end
採用された回答
Star Strider
2018 年 1 月 27 日
One way to approach this (and the one that seems most compatible with your homework assignment) is to use a while loop with an internal counter ‘n’ that keeps rolling the die (and increments) until ‘k’ appears. The loop then stops and reports ‘n’.
17 件のコメント
Mel Hernandez
2018 年 1 月 27 日
Oh my gosh, yes! That's what I need. So I'm thinking:
function [n] = firstRollK(K)
while true
n=randi(6)
if n==K
break
end
end
end
I'm not sure. I'm just very frustrated because I know what's going on, but I can't put it to code. Thank you very much for your help.
Walter Roberson
2018 年 1 月 27 日
You missed the counting part.
Mel Hernandez
2018 年 1 月 27 日
Oh. Um, what is that exactly? The 'counter' is what you're saying? And, I guess where would that go?
Star Strider
2018 年 1 月 27 日
I was thinking of something like this:
function k = firstRollK(K)
k = 0;
n = 0;
while n ~= K
k = k + 1;
n = randi(6);
end
end
Then call it as:
Kfirst = firstRollK(5)
Here, ‘k’ is the counter, and ‘n’ is the result you’re testing.
It worked in my experiments.
Don’t worry about being frustrated about not being able to code your thoughts — it’s still something I deal with!
Mel Hernandez
2018 年 1 月 27 日
Okay, so this:
Kfirst = firstRollK(5)
function k = firstRollK(K)
k = 0;
n = 0;
while n ~= K
k = k + 1;
n = randi(6);
end
end
I have a few questions. I ran it, and it keeps giving me the answer '5.' I'm guessing that's not supposed to happen. I don't understand why 'k' and 'n' are being told to be 0.
And thank you for being so understanding. It really means a lot that you're helping.
Star Strider
2018 年 1 月 27 日
My pleasure.
When I ran it, it didn’t repeatedly give 5 as the answer. It gave anything from 1 to 14.
If you are using R2016a or earlier, check to be certain that you have saved it as firstRollK.m, and then have called it correctly.
In R2016b or later allow functions in script files. However, functions must be at the end of the file. See the relevant documentation for details.
Mel Hernandez
2018 年 1 月 27 日
I cleared MATLAB in hopes of maybe fixing it, and I think I just made it worse. I ran the code as it was above, and now, it's giving me 'error' results. Thank you again for your help.
Star Strider
2018 年 1 月 27 日
My pleasure.
See my previous Comment, since I was likely typing it at the same time you were typing yours.
Mel Hernandez
2018 年 1 月 27 日
Okay so I ran the file that looks like this:
function [k] = firstRollK(K)
k=0;
n=0;
while n ~= K
k = k+1;
n = randi(6);
end
Kfirst = firstRollK(5)
end
Right? I ran that, and it says: "Error using firstRollK (line 5) Not enough input arguments."
I'm using the 2015a version.
Star Strider
2018 年 1 月 27 日
Not quite right.
You have to save:
function k = firstRollK(K)
k = 0;
n = 0;
while n ~= K
k = k + 1;
n = randi(6);
end
end
as: firstRollK.m.
Then call it from your script as:
Kfirst = firstRollK(5)
Run it several times. You will get different values for ‘Kfirst’ in different calls to it.
Mel Hernandez
2018 年 1 月 27 日
Ohhhhh okay. I got it! It worked. Yay! I'm trying to limit it to only numbers 1-6, and I think randi(6) did that. I don't understand why I'm getting 14.
Star Strider
2018 年 1 月 27 日
Great!
You are getting 14 because it took 14 ‘tosses’ of the die before it came up 5 (or whatever number you wanted it to produce). This is to be expected, and is likely the purpose of the assignment.
The probability of the one die coming up with any particular number on any one ‘roll’ is (1/6).
Walter Roberson
2018 年 1 月 27 日
14 rolls
Mel Hernandez
2018 年 1 月 27 日
Oh my gosh. Okay. That makes so much sense. But quick question. Why k and n equal 0? I just want to be sure why on that.
And I'm going to cry of happiness. Thank you so so much. To the both of you. It means a lot.
Star Strider
2018 年 1 月 27 日
As always, my pleasure.
It’s necessary to initialize ‘n’ to 0 (or any number other than ‘K’) because the while loop tests for it. Setting ‘k’ to 0 is necessary because it counts the iterations (dice rolls) necessary for ‘n’ to equal ‘K’, and increments in the loop.
I apologize for the delay in responding. It was bedtime for me here. Morning now.
Mel Hernandez
2018 年 1 月 28 日
Thank you very much!
Star Strider
2018 年 1 月 28 日
As always, my pleasure!
Have fun!
その他の回答 (1 件)
Walter Roberson
2018 年 1 月 27 日
For work like this, you can proceed in two basic ways:
1) roll the dice one at a time until you get the target number, counting rolls as you go. Repeat this for the number of experiments.
2) roll as many dice as there are experiments, some arbitrary number of times each (e.g.,50.) Then with that data array on hand, for each column, head down until you reach the target number, counting as you go. Note though that unless you take further steps, this is not as robust, because you might not happen to see any occurrences of the target number with the arbitrary number of rolls. There are tricks to vectorize the counting.
In a quick simulation I did with 100000 experiments, the maximum number of rolls to reach "3" was 70.
カテゴリ
ヘルプ センター および File Exchange で Optimization についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
