next up previous index
Next: Generating a Random Sample Up: Octave Programs Previous: Univariate General Linear Models   Index

Click for printer friendely version of this HowTo


Generating Normal(0,1) Random Variables

function normals=generate_normals(n)
  ## NOTE: if 'n' is odd, then you'll get floor(n) random variables
  for i = 1:(n/2)
    u1 = rand();
    u2 = rand();

    r = sqrt(-2*log(u1));
    theta = 2 * pi * u2;

    normals((i*2)-1) = r * cos(theta);
    normals(i*2) = r * sin(theta);
  endfor
endfunction



Frank Starmer 2004-05-19
>