function X=rvgen(f, M, N, seed) % rvgen(f, M, N, seed) Generates a matrix of N samples of M experiments % from a given probability or cumulative function. Each row of the % matrix represent an experiment, and each column represents a sample. % The seed sets the random number generator seed. So the resulting % matrix is M by N. Trials may be duplicated by using the same seed. % % rvgen would rather have a cumulative function. if iscf(f)==0 & ispf(f)==0 error('invalid probability function') end if ispf(f)==1 f=getcf(f); end x=xvals(f); p=pvals(f); rand('seed', seed); slope=1; X=zeros(M, N); for t=1:M, for i=1:N, R=rand; j=round(R*length(x)+0.5); if j>length(x) j=length(x); end if R>=p(j) while j=p(j) if j<=length(p)-10 if R>p(j+10) j=j+10; else j=j+1; end else j=j+1; end end X(t, i)=x(j); else j=j-1; while j>0 & R10 if R0 j=j+1; elseif j==0 if R>p(1) j=1; else j=0; end end X(t, i)=x(j+1); end end end