how to write a function that ask to outcome the probability of rolling i dice and getting number 5 from the first time?

2 次查看(过去 30 天)
im trying to write a function that ask the probability of getting number five from rolling a dice ( 1 - 6)
is answer is suppose to be 1\6

回答(1 个)

Image Analyst
Image Analyst 2022-12-10
Are you wanting to wanting to solve it by a Monte Carlo simulation? Then simply use randi and histogram.
This looks like a homework problem. If you have any questions ask your instructor or read the link below to get started:
Obviously we can't give you the full solution because you're not allowed to turn in our code as your own.
  1 个评论
Image Analyst
Image Analyst 2022-12-10
And we don't recomment using "i" as a variable name since that's the imaginary constant sqrt(-1). So if you roll "i" dice (like i = 8 or something), what does "getting number 5 from the first time" mean? Do you mean like all dice are labeled 1 to i and you're only interested in dice #1's value and none of the rest of the (i-1) dices values? And how many times are you going to roll this set of i dice? You'd need to do it lots of times to get a probability.
i = 3; % Would be better if you called this numberOfDice. This is the number of dice rolled per experiment.
numExperiments = 1000; % However many times you're going to roll the set of i dice.
rolls = randi(6, numExperiments, i)
rolls = 1000×3
5 5 3 1 6 6 1 4 6 2 6 5 5 1 4 2 5 5 2 5 1 2 5 1 2 4 1 1 6 1

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by