Next: Copying random number generator state, Previous: Auxiliary random number generator functions, Up: Random Number Generation
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_TYPEandGSL_RNG_SEEDand uses their values to set the corresponding library variablesgsl_rng_defaultandgsl_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_seedThe environment variable
GSL_RNG_TYPEshould be the name of a generator, such astausormt19937. The environment variableGSL_RNG_SEEDshould contain the desired seed value. It is converted to anunsigned long intusing the C library functionstrtoul.If you don't specify a generator for
GSL_RNG_TYPEthengsl_rng_mt19937is used as the default. The initial value ofgsl_rng_default_seedis 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