Rolling two dice with user defined functions
2 views (last 30 days)
Show older comments
Hey Everyone! I am looking to get some help with an assignment that I can not seem to figure out. We are supposed to create two user defined functions to simulate two dice being rolled 300 times. For my first function i currently have:
function [s_out] = KJL_roll(s_in)
s_out = floor(6*rand(1)+1);
end
which simulates a single dice being rolled. We are supposed to take that function and use it to simulate two dice being rolled. This is where I am stuck because I have created a way to do it that looks like this:
function [s_out] = KJL_rolls(s_in)
s_out_1 = KJL_roll;
s_out_2 = KJL_roll;
s_out = s_out_1 + s_out_2;
end
Is there a better way to do this using a loop statement? And how would I go about making this run 300 times while counting how many 8s appear during the runs?
Thanks in advance!
1 Comment
dpb
on 8 Apr 2014
Being as how it's HW, I'll ask some leading questions in return rather than posting direct solution(s)--
A) consider doc randi
B) what's the problem you see in wrapping the above in a counted for...end loop, specifically?
C) outside that what if you were to return more than one value per call? Can you see how that might lead to a useful end?
D) doc hist % and friends
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!