Code covered by the BSD License

# Random numbers from a user defined piecewise polynomial distribution

### Luigi Sanguigno (view profile)

RANDPDFS returns pseudorandom values drawn from an arbitrary distribution defined by the pp-form.

randpdfsdoc.m
```%% Randpdfs
% Distributed pseudorandom numbers from an user defined distribution function.

%% Syntax
% r=randpdfs(ppdf,n)
% r=randpdfs(ppdf,n,m)
% r=randpdfs(ppdf,[n,m])

%% Description
% r=randpdfs(ppdf,n) returns an n-by-n matrix containing pseudorandom values
% drawn from an user defined probability distribution.
% The probability distribution function is defined by using a pp-form
% which can be easily created using spline matlab command.
% The syntax to create random matrix is the same one used by rand command.

x=0:.05:2*pi; y=1+.5*cos(3*x-pi);
ppdf=spline(x,y);

%%
% All the information required to compute the random sequence are extracted
% from the pp-form structure, in particular the lowest and the highest
% generated numbers corresponds to the first and last break of the pp form
% respectively.
% randpdfs uses fnint to compute the cumulative distribution function,
% therefore spline toolbox has to be accessible to allow randpdfs working
% properly.

%% Example
% Generate values from an arbitraty distribution on the interval [0 2*pi]

[r,varargout]=randpdfs(ppdf,[1,50000]);
[Ncounts,binpos]=hist(r,30);
xx=0:.01:2*pi;
clf; hold on
plot(xx,ppval(ppdf,xx))
stem(binpos,Ncounts*mean(ppval(ppdf,binpos)./Ncounts),'o-r')
hold off
xlim([xx(1),xx(end)])
legend({'Assigned','Computed'},'Location','SouthEast','FontSize',12)
set(gca,'FontSize',12)
xlabel('Values','FontSize',12); ylabel('Occurence','FontSize',12)