Thread: gsl_rng_gaussian?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    gsl_rng_gaussian?

    I'm looking into using the gsl library random number generators...

    The program I have at hand produces uniform random numbers in the range [0.0, 1.0),

    Code:
         #include <stdio.h>
         #include <gsl/gsl_rng.h>
         
         int
         main (void)
         {
           const gsl_rng_type * T;
           gsl_rng * r;
         
           int i, n = 10;
         
           gsl_rng_env_setup();
         
           T = gsl_rng_default;
           r = gsl_rng_alloc (T);
         
           for (i = 0; i < n; i++) 
             {
               double u = gsl_rng_uniform (r);
               printf ("%.5f\n", u);
             }
         
           gsl_rng_free (r);
         
           return 0;
         }
    this code outputs:
    $ ./a.out

    0.99974
    0.16291
    0.28262
    0.94720
    0.23166
    0.48497
    0.95748
    0.74431
    0.54004
    0.73995
    in this particular case.

    How can I modify it so it can generate random numbers based on gaussian distribution? ( I don't know enough to decipher the code above in parts)

    double gsl_ran_gaussian (const gsl_rng * r, double sigma)

    "This function returns a Gaussian random variate, with mean zero and standard deviation sigma. The probability distribution for Gaussian random variates is,

    p(x) dx = {1 \over \sqrt{2 \pi \sigma^2}} \exp (-x^2 / 2\sigma^2) dx

    for x in the range -\infty to +\infty. Use the transformation z = \mu + x on the numbers returned by gsl_ran_gaussian to obtain a Gaussian distribution with mean \mu. This function uses the Box-Mueller algorithm which requires two calls to the random number generator r. "

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So do something like

    double u = gsl_ran_gaussian (r, 0.2);
    And get a Red Curve

    double u = gsl_ran_gaussian (r, 5.0);
    And get a Blue Curve
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    On top of the question (thanks Salem), I have trouble placing the list of randomly generated numbers into an already dynamically allocated array.

    so I will have "u", which is this list of randomly generated numbers, and I want to put it in the array called "a", and I believe a for loop is needed in this case. I will have N integers in the list "u", and I want to place the first number in u to a[0], second number in a[1]...Nth number in a[N-1].

    Code:
    int i;
    for(i=0, i < N, i++)
    {
    
    }
    return (i);
    What do I add in the brackets?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't know how to use arrays?
    Code:
    int array[3];
    ...
    array[ 0 ] = something;
    arrry[ 1 ] = somethingelse;
    array[ 2 ] = array[ 1 ] + array[ 2 ];
    Or a loop...
    Code:
    for( x = 0; x < 3; x++ )
        array[ x ] = somethingorother;
    Don't you have any books on C? Any beginner level C book should cover arrays.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed