Hi, i try to program a white noise generator with gaussian distribution. When i first used the rand() function i connected the 12-Bit DA-converter to a speaker. A periodic timer interrupt (about 400kHz) gets a rand number and puts it to the DA-converter.
I expected white noise, but wasn't satiesfied, i could hear a hum inside the white noise. The random numbers seemed to have some periodic pattern with a high repeat rate inside. I think with a spectrum-analyzer there would be some extra lines inside the spectrum. Now i wrote my own rand() function.
//*************************************************************************** int my_rand(int init_rand) { static unsigned int Z; if (init_rand) Z = init_rand; if (Z & 0x80000000) { Z<<=1; Z^=0x04C11DB7;//i saw it in a dream ;-) } else { Z<<=1; } return Z; } //***************************************************************************
After once beeing initialised with a value not zero it sounds great.
But is it mathematical correct?
After how many cycles does the initial value return?
Can i get a pseudo gaussian distribution with the following?
Z = my_rand(0); K = ((Z>>11 ^ Z>>22) & 0x3FF) +((Z>>11 ^ Z ) & 0x3FF) +((Z>>22 ^ Z>>5 ) & 0x3FF) +((Z ^ Z>>17) & 0x3FF); DAC0DAT = K<<16;//the 12bit-ADuC7026-DA-converter
There must be a more elegant method to have gaussian distribution!
Thanks for advice.
But is it mathematical correct? After how many cycles does the initial value return?
No offense, but if you're asking these questions instead of answering them, then you're not qualified to design a PRNG from scratch.
Random number generation rapidly gets to be a very complicated topic. Knuth is a good place to start, or ask Google about "random number generation".
Playing the output of a RNG through speakers is actually a pretty good rough test of its uniformity if you don't have your hard-core statistical toolbox handy. People are good at hearing the errors as hums or tones in the nearly white noise. Most simple linear PNGs will produce audible artifacts.
The Central Limit Theorem tells you that you can approximate a normal (Gaussian) distribution with the sum of any other distribution. So one way to create a normally distributed RNG is simply to use your uniform RNG and sum a series of results.