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

Discussion in 'C++' started by fAnSKyer, Jan 3, 2007.

  1. fAnSKyer

    fAnSKyer Guest

    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.
    fAnSKyer, Jan 3, 2007
    #1
    1. Advertising

  2. 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?


    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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 3, 2007
    #2
    1. Advertising

  3. fAnSKyer

    Puppet_Sock Guest

    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?


    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
    Puppet_Sock, Jan 3, 2007
    #3
  4. fAnSKyer

    fAnSKyer Guest

    Thanks a lot ^_^ :p

    "Puppet_Sock дµÀ£º
    "
    > 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?

    >
    > 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
    fAnSKyer, Jan 3, 2007
    #4
  5. fAnSKyer

    Lionel B Guest

    Re: How to generate a Guassion Sample data? used for MonteCarlomethod

    On Wed, 03 Jan 2007 10:05:00 -0800, fAnSKyer wrote:

    > 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;
    }

    --
    Lionel B
    Lionel B, Jan 4, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jake
    Replies:
    0
    Views:
    530
  2. fAnSKyer

    How to generate a Guassion Sample data?

    fAnSKyer, Jan 3, 2007, in forum: C Programming
    Replies:
    4
    Views:
    420
    Martin Ambuhl
    Jan 3, 2007
  3. mahesh
    Replies:
    3
    Views:
    290
    Jim Langston
    Jan 23, 2007
  4. Casey Hawthorne
    Replies:
    1
    Views:
    696
    Arne Vajhøj
    Mar 18, 2009
  5. Tim Chase
    Replies:
    0
    Views:
    63
    Tim Chase
    Feb 16, 2014
Loading...

Share This Page