How to generate a Guassion Sample data? used for MonteCarlo method

F

fAnSKyer

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.
 
V

Victor Bazarov

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?

No, not currently. TR1 Library extension has a couple random
distributions (Bernoulli, Poisson, geometric), and you need the
'normal', I believe. If your compiler provides TR1 (or if you get
an implementation elsewhere), see std::tr1::normal_distribution.

V
 
P

Puppet_Sock

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?

Not built in for most compilers. Also, the built in random function
isn't very good. Probably you won't want to use it for anything
more important than games. And maybe not even then.

The basic idea is, you use the shape of the distribution you
want to generate, and modify the values from a uniform
generator to put them into the range you want.

For an easy to understand introduction to the topic, take a
look at _Numerical Recipes in C_. But be sure to keep in
mind that it should only be treated as an introduction.
There is quite a large literature on random num generation.

Depending on your requirements, you may need a very good
random num generator. You may need to worry about such
things as the repeat cycle, the stride, various statistics, etc.
The words "Monte Carlo" in your subject line tend to make
me think you need at least a medium good generator.

You should look round the net for some libs that provide better
random nums. There are several, and several free ones.
Be sure to read carefully the promises that the docs that
come with such libs make. Or fail to make. And you should
hunt round the net for revues of any lib before you use it.
Socks
 
F

fAnSKyer

Thanks a lot ^_^ :p

"Puppet_Sock дµÀ£º
"
Not built in for most compilers. Also, the built in random function
isn't very good. Probably you won't want to use it for anything
more important than games. And maybe not even then.

The basic idea is, you use the shape of the distribution you
want to generate, and modify the values from a uniform
generator to put them into the range you want.

For an easy to understand introduction to the topic, take a
look at _Numerical Recipes in C_. But be sure to keep in
mind that it should only be treated as an introduction.
There is quite a large literature on random num generation.

Depending on your requirements, you may need a very good
random num generator. You may need to worry about such
things as the repeat cycle, the stride, various statistics, etc.
The words "Monte Carlo" in your subject line tend to make
me think you need at least a medium good generator.

You should look round the net for some libs that provide better
random nums. There are several, and several free ones.
Be sure to read carefully the promises that the docs that
come with such libs make. Or fail to make. And you should
hunt round the net for revues of any lib before you use it.
Socks
 
L

Lionel B

When using random we can get a uniform data sample.

The "Mersenne Twister" is about as good as it gets for uniform (pseudo)
random number generation - it is fast, has a very long period and passes
most statistical tests:

http://en.wikipedia.org/wiki/Mersenne_twister
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
But how to transfer

this sample in to a Guassion Sample? Thanks? Did C or C++ provide any
function to do this?

No, but here is an implementation of the Box-Muller algorithm for
generating Gaussian random deviates that will do the job (note that this
version is not thread-safe). See Numerical Recipes in C and:

http://en.wikipedia.org/wiki/Box-Muller_transform

for details.

// rand() should return a random double uniform on [0,1]

double gasdev()
{
static bool iset;
static double gset;
double fac,rsq,v1,v2;
if (iset) {
iset=false;
return gset;
}
do {
v1=2.0*rand()-1.0;
v2=2.0*rand()-1.0;
rsq=v1*v1+v2*v2;
} while (rsq >= 1.0 || rsq == 0.0);
fac=std::sqrt(-2.0*std::log(rsq)/rsq);
gset=v1*fac;
iset=true;
return v2*fac;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top