Next: , Previous: The Random Number Generator Interface, Up: Random Number Generation   [Index]

18.3 Random number generator initialization

Function: gsl_rng * gsl_rng_alloc (const gsl_rng_type * T)

This function returns a pointer to a newly-created instance of a random number generator of type T. For example, the following code creates an instance of the Tausworthe generator,

gsl_rng * r = gsl_rng_alloc (gsl_rng_taus);

If there is insufficient memory to create the generator then the function returns a null pointer and the error handler is invoked with an error code of GSL_ENOMEM.

The generator is automatically initialized with the default seed, gsl_rng_default_seed. This is zero by default but can be changed either directly or by using the environment variable GSL_RNG_SEED (see Random number environment variables).

The details of the available generator types are described later in this chapter.

Function: void gsl_rng_set (const gsl_rng * r, unsigned long int s)

This function initializes (or ‘seeds’) the random number generator. If the generator is seeded with the same value of s on two different runs, the same stream of random numbers will be generated by successive calls to the routines below. If different values of s >= 1 are supplied, then the generated streams of random numbers should be completely different. If the seed s is zero then the standard seed from the original implementation is used instead. For example, the original Fortran source code for the ranlux generator used a seed of 314159265, and so choosing s equal to zero reproduces this when using gsl_rng_ranlux.

When using multiple seeds with the same generator, choose seed values greater than zero to avoid collisions with the default setting.

Note that the most generators only accept 32-bit seeds, with higher values being reduced modulo 2^32. For generators with smaller ranges the maximum seed value will typically be lower.

Function: void gsl_rng_free (gsl_rng * r)

This function frees all the memory associated with the generator r.
