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.
a white noise generator with gaussian distribution.
That's a self-contradictory goal. Noise from a gaussian PRNG is anything but white.
I think with a spectrum-analyzer there would be some extra lines inside the spectrum.
What's there to "think"? You don't need a spectrum analyzer --- just write that stream of PRNG outputs to a file and analyze it. Or better yet, run that generator through an actual PRNG test suite.
Now i wrote my own rand() function.
Bad idea. Seriously. To paraphrase Don E. Knuth on this subject: the odds that a randomly put-together PRNG turns out to be a decent one are negligible. Good PRNGs just don't appear in dreams.
Good PRNGs just don't appear in dreams.
'Good' is a relative term, in my opinion: "Good PRNGs just don't appear"
The only way to be random is getting the 'pseudo' out of it.
If you do not need more than one random number, a good method is to run a timer free (auto-reload of zero) and read it when a key is pressed. I have a random function based on the content of a free running timer when the first character of a fully asynchronous transmission from the outside happen.
Erik