Next: Rather Large Matrices
Up: Octave Programs
Previous: Generating Normal(0,1) Random Variables
Index
Click for printer friendely version of this HowTo
Generating a Random Sample from a Discrete Distribution
function discretes=generate_discretes(n)
## First map the distribution to the interval [0,1].
dist(1) = 0.25;
dist(2) = 0.5;
dist(3) = 0.25;
for i = 1:(n)
u1 = rand();
total = 0;
index = 1;
for j = 1:length(dist)
total = total + dist(j);
if (u1 < total)
index = j;
break;
endif
endfor
u2 = rand();
discretes(i) = u2 + (index - 1);
endfor
endfunction
Frank Starmer
2004-05-19