new; format /rd 10,8; rndseed 1166; /*Truncated Normal on [0,.5]*/ timedt; n = 100; x = zeros(n,1); count = 1; jj = 1; do until jj gt n; j = 0; do until j eq 1; u_i = rndu(1,1); z_i = rndn(1,1); idz = (z_i .ge 0) .and (z_i .le .5); stat = 2.08365*exp(-.5*z_i^2)*idz; stat = stat/(5.22302*pdfn(z_i)); if stat .ge u_i; x[jj] = z_i; j = 1; endif; count = count + 1; endo; jj = jj+1; endo; x; count;n/count; timedt; x = zeros(n,1); count = 1; jj = 1; do until jj gt n; j = 0; do until j eq 1; u_i = rndu(1,1); z_i = rndu(1,1)*.5; stat = 2.08365*exp(-.5*z_i^2); stat = stat/(2*1.04183); if stat .ge u_i; x[jj] = z_i; j = 1; endif; count = count + 1; endo; jj = jj+1; endo; x; count;100/count; timedt; /*Truncated Normal on [5,8]*/ timedt; n = 100; x = zeros(n,1); count = 1; jj = 1; do until jj gt n;count; j = 0; do until j eq 1; u_i = rndu(1,1); z_i = rndn(1,1); idz = (z_i .ge 5) .and (z_i .le 8); stat = inv(cdfn(8)-cdfn(5))*pdfn(z_i)*idz; stat = stat/(inv(cdfn(8)-cdfn(5))*pdfn(z_i)); /*Better Way to do this*/ /*stat = idz*/ if stat .ge u_i; x[jj] = z_i; j = 1; endif; count = count + 1; endo; jj = jj+1; endo; x; count;n/count; timedt; x = zeros(n,1); count = 1; jj = 1; do until jj gt n; j = 0; do until j eq 1; u_i = rndu(1,1); z_i = rndu(1,1)*3+5; stat = inv(cdfn(8)-cdfn(5))*pdfn(z_i); stat = stat/(15.3799/3); if stat .ge u_i; x[jj] = z_i; j = 1; endif; count = count + 1; endo; jj = jj+1; endo; x; count;100/count; timedt; x = zeros(n,1); count = 1; jj = 1; do until jj gt n; j = 0; do until j eq 1; u_i = rndu(1,1); z_i = 5-.2*ln(1-rndu(1,1)); idz = (z_i .ge 5) .and (z_i .le 8); stat = inv(cdfn(8)-cdfn(5))*pdfn(z_i)*idz; stat = stat/(1.02532*5*exp(-5*(z_i-5))); if stat .ge u_i; x[jj] = z_i; j = 1; endif; count = count + 1; endo; jj = jj+1; endo; x; count;100/count; timedt; end;