Want to simulate a sequence of results from rolling a nonhomogeneous die

1 view (last 30 days)
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.

Answers (1)

James Tursa
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))
  1 Comment
Alfred Ftoni
Alfred Ftoni on 22 Feb 2017
Hey James thanks for the reply. So I need what you suggested, now are you saying I should use the codes you gave me and try and put them into my function? I tried that and once I ran the code, I get back 2 value. and every time I hit run, I get new values back. I am not sure if I did it right or not. I am not very good at using Matlab and your feedback is very helpful.

Sign in to comment.

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!