title: Where do the magic numbers in this normal RNG come from?
body: <p>A popular method to generate normally distributed numbers comes from the ratio of uniform deviates technique derived by Kinderman and Monahan and improved by Leva. <a href="https://stackoverflow.com/questions/8739851/drawing-random-number-from-a-standard-normal-distribution-using-the-standard-ran">The method is compact and requires little memory</a>:</p>
  
  <pre><code>static Random r = new Random();
  static double Normal(double mu, double sig)
  {
      double u, v, x, y, q;
      do
      {
          u = r.NextDouble();
          v = 1.7156 * (r.NextDouble() - 0.5);
          x = u - 0.449871;
          y = Math.Abs(v) + 0.386595;
          q = Math.Sqrt(x) + y * (0.19600 * y - 0.25472 * x);
      } while (q &gt; 0.27597 &amp;&amp; (q &gt; 0.27846 || Math.Sqrt(v) &gt; -4.0 * Math.Log(u) * Math.Sqrt(u)));
      return mu + sig * v / u;
  }
  </code></pre>
  
  <p>The <a href="https://pdfs.semanticscholar.org/85ee/3008e18f41a76b975a780820c30e1b8ae29a.pdf" rel="nofollow noreferrer">Leva paper</a> describes the equations and solutions, but it isn't clear to me how the values were determined:</p>
  
  <pre><code>acceptance region: v^2 &lt; -4*u^2*ln(u)
  
  inner boundary: v = ± u*sqrt(5 - 5.136102*u)
  outer boundary: v = ± u*sqrt(1.4 + 1.03696/u)
  
  quadratic form: Q(u,v) = (u-s)^2 - b*(u-s)*(v-t) + a*(v-t)^2
  
  s  = 0.449871
  t  = -0.386595
  r1 = 0.27597
  
  a  = 0.19600
  b  = 0.25472
  r2 = 0.27846
  </code></pre>
  
  <p>Where do the magic numbers for <code>v, s, t, r1, a, b, r2</code> come from?</p>