To roll a dice simulator
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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일
0 개 추천
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일
0 개 추천
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
