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
I beleive that the rand() function is supposed to model a uniform distribution, not a Gaussian one. Tell us what you are trying to achieve and we might be able to help. If you are interested in the subject, read up on it. You could start with a Wikipedia article:
en.wikipedia.org/.../White_noise
- mike
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.