(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
<[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]>> 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
_______________________________________________
Help-gsl mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-gsl