470,848 Members | 1,214 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

How to generate a Guassion Sample data?

When using random we can get a uniform data sample. But how to transfer
this sample in to a Guassion Sample? Thanks? Did C or C++ provide any
function to do this?

Thanks a lot.

Cheers. fAnS.

Jan 3 '07 #1
4 2392
fAnSKyer said:
When using random we can get a uniform data sample. But how to transfer
this sample in to a Guassion Sample? Thanks? Did C or C++ provide any
function to do this?
http://c-faq.com - see question 13.20

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 3 '07 #2
"fAnSKyer" <fa******@gmail.comwrites:
When using random we can get a uniform data sample. But how to transfer
this sample in to a Guassion Sample? Thanks? Did C or C++ provide any
function to do this?
This is in the C FAQ.

13.20: How can I generate random numbers with a normal or Gaussian
distribution?

A: Here is one method, recommended by Knuth and due originally to
Marsaglia:

#include <stdlib.h>
#include <math.h>

double gaussrand()
{
static double V1, V2, S;
static int phase = 0;
double X;

if(phase == 0) {
do {
double U1 = (double)rand() / RAND_MAX;
double U2 = (double)rand() / RAND_MAX;

V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
} while(S >= 1 || S == 0);

X = V1 * sqrt(-2 * log(S) / S);
} else
X = V2 * sqrt(-2 * log(S) / S);

phase = 1 - phase;

return X;
}

See the extended versions of this list (see question 20.40) for
other ideas.

References: Knuth Sec. 3.4.1 p. 117; Marsaglia and Bray,
"A Convenient Method for Generating Normal Variables";
Press et al., _Numerical Recipes in C_ Sec. 7.2 pp. 288-290.

--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Jan 3 '07 #3
Thanks

That's a great help :P
"Ben Pfaff д
"
"fAnSKyer" <fa******@gmail.comwrites:
When using random we can get a uniform data sample. But how to transfer
this sample in to a Guassion Sample? Thanks? Did C or C++ provide any
function to do this?

This is in the C FAQ.

13.20: How can I generate random numbers with a normal or Gaussian
distribution?

A: Here is one method, recommended by Knuth and due originally to
Marsaglia:

#include <stdlib.h>
#include <math.h>

double gaussrand()
{
static double V1, V2, S;
static int phase = 0;
double X;

if(phase == 0) {
do {
double U1 = (double)rand() / RAND_MAX;
double U2 = (double)rand() / RAND_MAX;

V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
} while(S >= 1 || S == 0);

X = V1 * sqrt(-2 * log(S) / S);
} else
X = V2 * sqrt(-2 * log(S) / S);

phase = 1 - phase;

return X;
}

See the extended versions of this list (see question 20.40) for
other ideas.

References: Knuth Sec. 3.4.1 p. 117; Marsaglia and Bray,
"A Convenient Method for Generating Normal Variables";
Press et al., _Numerical Recipes in C_ Sec. 7.2 pp. 288-290.

--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Jan 3 '07 #4
fAnSKyer wrote:
When using random we can get a uniform data sample. But how to transfer
this sample in to a Guassion Sample? Thanks? Did C or C++ provide any
function to do this?
It is not only a good idea to check the FAQ before posting to a
newsgroup, but it is the behavior expected from civilized posters. In
this case you would have found question 13.20 at
<http://c-faq.com/lib/gaussian.html>.
Jan 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Andrew | last post: by
1 post views Thread by Nettar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.