function y = rhalfnormar(n,mu) % ************************************************************************* % % FILE: rhalfnormar.m % % Date Created: March 11, 2004 % Author: Mark E. Irwin % % Contents: Generates random deviates from the half normal distribution % by acceptance-rejection method % % Revision History % Date Name Changes/Reasons % % ************************************************************************* % % Input: % % n: number of random deviates % mu: mean of exponential distribution used for proposal % % Output: % % y = vector of random deviates % % ************************************************************************* y = zeros(1,n); c = mu*exp(0.5/mu^2)* sqrt(2/pi); for i = 1:n accept = 0; while(~accept) % Generate proposal x = -mu*log(rand(1)); % Calculate acceptance probability r = exp(-(x - 1/mu)^2 / 2); % Coin flip for acceptance accept = (rand(1) <= r); end y(i) = x; end