Code covered by the BSD License  

Highlights from
Many observations from arbitrary PDF

Many observations from arbitrary PDF


Jack Peterson


Generate many observations from an arbitrary PDF.

function f = randarbmulti(x,y,N)
% RANDARBMULTI generates N random numbers from an arbitrary PDF, defined by
% vectors x and y.  Modified from randarb.m (Dave Dykes) at:
% Jack Peterson (, 4/21/2013

cdf_y = cumsum(y);
sum_y = sum(y);

for j = 1:N
	% Compute a uniform random number between 0 and sum(y)
	randx = sum_y*rand();
	% Find where the number lies on a conceptual line comprised of "segments" of
	% length y
	i = 1;
	while cdf_y(i) < randx
		i = i + 1;
	% Return the x value corresponding to the y value we "landed on."
	f(j) = x(i);

Contact us