Next: Copying random number generator state, Previous: Auxiliary random number generator functions, Up: Random Number Generation [Index]
The library allows you to choose a default generator and seed from the
environment variables GSL_RNG_TYPE and GSL_RNG_SEED and
the function gsl_rng_env_setup. This makes it easy try out
different generators and seeds without having to recompile your program.
This function reads the environment variables GSL_RNG_TYPE and
GSL_RNG_SEED and uses their values to set the corresponding
library variables gsl_rng_default and
gsl_rng_default_seed. These global variables are defined as
follows,
extern const gsl_rng_type *gsl_rng_default extern unsigned long int gsl_rng_default_seed
The environment variable GSL_RNG_TYPE should be the name of a
generator, such as taus or mt19937. The environment
variable GSL_RNG_SEED should contain the desired seed value. It
is converted to an unsigned long int using the C library function
strtoul.
If you don’t specify a generator for GSL_RNG_TYPE then
gsl_rng_mt19937 is used as the default. The initial value of
gsl_rng_default_seed is zero.
Here is a short program which shows how to create a global
generator using the environment variables GSL_RNG_TYPE and
GSL_RNG_SEED,
#include <stdio.h>
#include <gsl/gsl_rng.h>
gsl_rng * r; /* global generator */
int
main (void)
{
const gsl_rng_type * T;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
printf ("generator type: %s\n", gsl_rng_name (r));
printf ("seed = %lu\n", gsl_rng_default_seed);
printf ("first value = %lu\n", gsl_rng_get (r));
gsl_rng_free (r);
return 0;
}
Running the program without any environment variables uses the initial
defaults, an mt19937 generator with a seed of 0,
$ ./a.out
generator type: mt19937 seed = 0 first value = 4293858116
By setting the two variables on the command line we can change the default generator and the seed,
$ GSL_RNG_TYPE="taus" GSL_RNG_SEED=123 ./a.out GSL_RNG_TYPE=taus GSL_RNG_SEED=123 generator type: taus seed = 123 first value = 2720986350