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 08:41:20 -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

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

ozgur a écrit :
Hello people;
I 'd ask your advice. I'm implementing a function in C++ which returns n
uniformly distributed doubles.My question is how to seed that function with
random seed in order to produce different random numbers when called again
and again within very short time intervals.When i test it it produces the
same output within close time intervals but works fine when i call it after
a second or something.If you give some code snippets for solution i can
figure it out more easily. Thanks in advance.I'm giving the code below:
**************************************
#include <ctime>
#include <cstdlib>
#include <gsl/gsl_rng.h>

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 long l = rand();

    gsl_rng_set(r, l);

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






reply via email to

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