% TRIRND2 Generate numbers from a 0-centered triangular distribution % % Density is f(x) = 1-abs(x) for x in [-1,1]. % function X=trirnd2(m,n) if nargin<1, m=1; end if nargin<2, n=m; m=1; end % Simple method: Take [0,1]-distributed triangular randoms % like in TRIRND. Then randomly (0.5 chance) either shift left % by 1 unit into the interval [-1, 0], or mirror left-right % inside the [0,1] interval % (There are other methods but this is easy.) U = unifrnd(0,1,m,n); X = sqrt(U); % Choices for shifting (V=1) or mirroring (V=2) V = randi(2,m,n); X(V==1) = X(V==1) - 1; X(V==2) = 1 - X(V==2);