help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] Uniform random number generation


From: Maxime Boissonneault
Subject: Re: [Help-gsl] Uniform random number generation
Date: Thu, 27 Nov 2008 11:21:57 -0500
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; fr; rv:1.8.1.18) Gecko/20081105 Thunderbird/2.0.0.18 Mnenhy/0.7.6.0

(Quasi)Random number generators are initiated by a seed. Then, each time they are called, they return a different number.

In fact, quasirandom number generators are computing quasirandom number following a well defined cyclic serie, but this serie has the properties of random numbers. The seed simply set the starting point in the serie.

This means that if you have the same seed, you will always get the same serie. It also means that quasirandom number generators have a period. If you would extract an infinite number of quasirandom numbers out of it, you would get a sequence of numbers that is repeated. This is however not a problem as long as the period is much longer than the number of numbers you extract. There are number generators that have a period of 10^20 and more. See the GSL documentation for more information.


Maxime

ozgur a écrit :


On Thu, Nov 27, 2008 at 3:41 PM, Maxime Boissonneault <address@hidden <mailto:address@hidden>> wrote:

    I would say to initialize the seed only once when the program
    starts (ie keep the random generator in a static variable). Also,
    you could use a more precise time function which returns the
    number of ticks (CPU cycles) and not the number of seconds or
    milliseconds.

    Maxime Boissonneault

I did something like this ( copying from the previous posts to the list; sorry for duplication, i had not done an elaborate search that time) using /dev/random . It works fine for now. But i did not understand a thing in your reply Maxime . If i keep random generator in a static variable, how can it return different serie each time when i call it? BTW thanks for the quick reply.

vector<double> rn_uniform(int n)
{
    vector<double> rn_array;
    // Define GSL RNG parameters.
    const gsl_rng_type * T;
    gsl_rng *r;
T = gsl_rng_taus2; // RNG type
    r = gsl_rng_alloc(T);// Allocate memory
    srand(time(NULL));
    unsigned int stb_seed = rand(); // System time based random seed
    unsigned int seed;
FILE *dev_random;
    dev_random = fopen("/dev/random","r");
    if (dev_random == NULL)
      {
        fprintf(stderr,"Can not open /dev/random - seeding failed\n");
        seed = stb_seed;
      }
    else
      {
        fread(&seed , sizeof(seed), 1, dev_random);
        fclose(dev_random);
      }
gsl_rng_set(r, seed); // Set the seed for GSL RNG int i;
    for(i=0; i< n; i++)
        {
            double u = gsl_rng_uniform(r);
            rn_array.push_back(u);
        }

    gsl_rng_free(r); // Free memory

    return rn_array;
};


--
Ozgur





reply via email to

[Prev in Thread] Current Thread [Next in Thread]