function [x_new,r] = unif_prop(x,xmin,xmax,h)

% Function to create a uniform proposal for x, with accept/reject
% ratio, where x is constrainted to lie in the interval [xmin,xmax].
% x_new ~ U(x-h,x+h)

x_new = (x-h) + 2*h*rand;
while (x_new < xmin || x_new > xmax) % x_new must belong to [xmin,xmax]
    x_new = (x-h) + 2*h*rand;
end

hl = h - (x-xmin);
if( hl < 0 ) 
    prob1 = h;
else
    prob1 = (x-xmin);
end
hu = h - (xmax-x);
if( hu < 0 )
    prob1 = (prob1 + h)/(2*h);
else
    prob1 = (prob1 + (xmax-x))/(2*h);
end

hl = h - (x_new-xmin);
if( hl < 0 ) 
    prob2 = h;
else
    prob2 = (x_new-xmin);
end
hu = h - (xmax-x_new);
if( hu < 0 )
    prob2 = (prob2 + h)/(2*h);
else
    prob2 = (prob2 + (xmax-x_new))/(2*h);
end

r = prob1/prob2;

    