simpler gaussian distribution code
authordiv0 <div0@f962a42d-fe04-0410-a3ab-8c8b0445ebaa>
Sun, 8 Nov 2009 12:23:38 +0000 (12:23 +0000)
committerdiv0 <div0@f962a42d-fe04-0410-a3ab-8c8b0445ebaa>
Sun, 8 Nov 2009 12:23:38 +0000 (12:23 +0000)
git-svn-id: svn://svn.icculus.org/nexuiz/trunk@8243 f962a42d-fe04-0410-a3ab-8c8b0445ebaa

data/qcsrc/common/util.qc

index e8d4d7b..655d17c 100644 (file)
@@ -1668,7 +1668,7 @@ float gsl_ran_gaussian_lastvalue;
 float gsl_ran_gaussian_lastvalue_set;
 float gsl_ran_gaussian(float sigma)
 {
-       float x, y, r2;
+       float a, b;
        if(gsl_ran_gaussian_lastvalue_set)
        {
                gsl_ran_gaussian_lastvalue_set = 0;
@@ -1676,16 +1676,10 @@ float gsl_ran_gaussian(float sigma)
        }
        else
        {
-               do
-               {
-                       x = -1 + 2 * random();
-                       y = -1 + 2 * random();
-                       r2 = x * x + y * y;
-               }
-               while(r2 > 1 || r2 == 0);
-               r2 = sqrt(-2 * log(r2) / r2);
-               gsl_ran_gaussian_lastvalue = x * r2;
+               a = random() * 2 * M_PI;
+               b = sqrt(-2 * log(random()));
+               gsl_ran_gaussian_lastvalue = cos(a) * b;
                gsl_ran_gaussian_lastvalue_set = 1;
-               return sigma * y * r2;
+               return sigma * sin(a) * b;
        }
 }