Gaussian White noise

References : Posted by Alexey Menshikov
Notes :
Code I use sometimes, but don't remember where I ripped it from.

- Alexey Menshikov
Code :
#define ranf() ((float) rand() / (float) RAND_MAX)

float ranfGauss (int m, float s)
{
   static int pass = 0;
   static float y2;
   float x1, x2, w, y1;

   if (pass)
   {
      y1 = y2;
   } else  {
      do {
         x1 = 2.0f * ranf () - 1.0f;
         x2 = 2.0f * ranf () - 1.0f;
         w = x1 * x1 + x2 * x2;
      } while (w >= 1.0f);

      w = (float)sqrt (-2.0 * log (w) / w);
      y1 = x1 * w;
      y2 = x2 * w;
   }
   pass = !pass;

   return ( (y1 * s + (float) m));
}

Comments
from : davidchristenATgmxDOTnet
comment : White Noise does !not! consist of uniformly distributed values. Because in white noise, the power of the frequencies are uniformly distributed. The values must be normal (or gaussian) distributed. This is achieved by the Box-Muller Transformation. This function is the polar form of the Box-Muller Transformation. It is faster and numeriacally more stable than the basic form. The basic form is coded in the other (second) post. Detailed information on this topic: http://www.taygeta.com/random/gaussian.html http://www.eece.unm.edu/faculty/bsanthan/EECE-541/white2.pdf Cheers David

from : nick[DOT]a[DOT]shaw[AT]btopenworld[DOT]com
comment : I'm trying to implement this in C#, but y2 isn't initialized. Is this a typo?

from : foo[AT]bar[DOT]de
comment : @nick: Way to late, but y2 will always be initialized as in the first run "pass" is 0 (i.e. false). The C# compiler just can't prove it.

from : asynth[AT]gmail[DOT]com
comment : David is wrong. The distribution of the sample values is irrelevant. 'white' simply describes the spectrum. Any series of sequentially independent random values -- whatever their distribution -- will have a white spectrum.

from : aleyuwo[AT]popp[DOT]eamale[DOT]com
comment : [url=http://dutasterideavodartonline.org/]dutasterideavodartonline.org.ankor[/url] <a href="http://online-purchaselevitra.net/">online-purchaselevitra.net.ankor</a> http://20mg-cheapest-pricelevitra.net/

from : oboqamego[AT]g[DOT]eamale[DOT]com
comment : [url=http://dutasterideavodartonline.org/]dutasterideavodartonline.org.ankor[/url] <a href="http://online-purchaselevitra.net/">online-purchaselevitra.net.ankor</a> http://20mg-cheapest-pricelevitra.net/