Want to simulate a sequence of results from rolling a nonhomogeneous die
1 view (last 30 days)
Show older comments
I need to simulate a sequence for a
non homogeneous die. I am given the following
and need to compute
and plot a graph with N vx XN and fr(a).
I tried using the die roll function.
function fr=dieroll(n)
pp1=1/2;
pp2=1/10;
pp3=1/10;
pp4=1/10;
pp5=1/10;
pp6=1/10;
U=rand(0,1);
U1=(U<pp1);
S=cumsum(U1);
fr=S./(0:1);
Please help.
0 Comments
Answers (1)
James Tursa
on 21 Feb 2017
Edited: James Tursa
on 21 Feb 2017
The expression rand(0,1) will produce a 0x1 sized matrix ... i.e., an empty matrix. That is not what you want. I will give you a hint on how to produce the random numbers from scratch.
Start with your probabilities (assumed to be positive and summing to 1):
p = [0.5 0.1 0.1 0.1 0.1 0.1];
Then create an associated cumulative probability vector with a 0 in the front (leaving out the last value which is assumed to be 1):
c = [0 cumsum(p(1:end-1))];
Now your hint is to look at what you get with the following code, and then work that into your program. I.e., try doing this several times, examine the displayed numbers to see what is going on, and then see if you can't get this worked into code that you can use for your assignment.
r = rand; disp(r); disp(sum(r>c))
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!