To roll a dice simulator
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 27 Jan 2018
0 votes
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.
Catégories
En savoir plus sur Optimization dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!