[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-gsl] Uniform random number generation
From: |
ozgur |
Subject: |
Re: [Help-gsl] Uniform random number generation |
Date: |
Thu, 27 Nov 2008 18:15:50 +0200 |
On Thu, Nov 27, 2008 at 3:41 PM, Maxime Boissonneault <
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