echo on % First, use the transformation method: N = 100000; Z = rand(N,1); delta = 0.05 bins = [0:delta:10]; Y = (Z<0.5).*sqrt(2*Z) + (Z>=0.5).*(2-sqrt(abs(2-2*Z))); [p,tmp] = hist(Y,bins); plot(bins,p/N/delta,[0 1 2], [0 1 0]); title('Transforming uniform to triangle'); legend('Histogram of transformed uniform','True exponential pdf') pause; % Now, use the sum of two uniforms method (convolution theorem): Z = rand(N,1); X = rand(N,1); Y = Z + X; [p,tmp] = hist(Y,bins); plot(bins,p/N/delta, [0 1 2], [0 1 0]); title('Transforming uniform to triangle'); legend('Histogram of transformed uniform','True exponential pdf') pause % Now, generate exponetial distribution from uniform N = 1e5; delta = 0.05 bins = [0:delta:10]; Z = rand(N,1); Y = -log(1-Z); [p,tmp] = hist(Y,[0:delta:10]); plot(bins,p/N/delta, bins, exp(-bins)); title('Transforming uniform to exponential'); legend('Histogram of transformed uniform','True exponential pdf') echo off