**Type :** gaussian noise generation

**References :** Posted by paul[at]expdigital[dot]co[dot]uk

**Notes :**

References :

Tobybears delphi noise generator was the basis. Simply converted it to C++.

Link for original is:

http://www.musicdsp.org/archive.php?classid=0#129

The output is in noise.

**Code :**

/* Include requisits */

#include <cstdlib>

#include <ctime>

/* Generate a new random seed from system time - do this once in your constructor */

srand(time(0));

/* Setup constants */

const static int q = 15;

const static float c1 = (1 << q) - 1;

const static float c2 = ((int)(c1 / 3)) + 1;

const static float c3 = 1.f / c1;

/* random number in range 0 - 1 not including 1 */

float random = 0.f;

/* the white noise */

float noise = 0.f;

for (int i = 0; i < numSamples; i++)

{

random = ((float)rand() / (float)(RAND_MAX + 1));

noise = (2.f * ((random * c2) + (random * c2) + (random * c2)) - 3.f * (c2 - 1.f)) * c3;

}

**Comments**

__from__ : none[AT]none[DOT]com

__comment__ : What's the difference between the much simpler noise generator:
randSeed = (randSeed * 196314165) + 907633515; out=((int)randSeed)*0.0000000004656612873077392578125f;
and this one? they both sound the same to my ears...

__from__ : unknown[AT]unknown[DOT]com

__comment__ : How can you change the variance (sigma)?

__from__ : bebul[AT]none[DOT]com

__comment__ : This is NOT a good code to generate Gaussian Noice. Look into:
(random * c2) + (random * c2) + (random * c2)
It is all nonsense! The reason of adding three numbers it the Central Limit Theorem to aproximate Gaussian distribution. But the random numbers inside must differ, which is not the case. The code on original link http://www.musicdsp.org/archive.php?classid=0#129 is correct.