Next: , Previous: Random Number Generator Performance, Up: Random Number Generation


17.13 Examples

The following program demonstrates the use of a random number generator to produce uniform random numbers in the range [0.0, 1.0),

     #include <stdio.h>
     #include <gsl/gsl_rng.h>
     
     int
     main (void)
     {
       const gsl_rng_type * T;
       gsl_rng * r;
     
       int i, n = 10;
     
       gsl_rng_env_setup();
     
       T = gsl_rng_default;
       r = gsl_rng_alloc (T);
     
       for (i = 0; i < n; i++) 
         {
           double u = gsl_rng_uniform (r);
           printf ("%.5f\n", u);
         }
     
       gsl_rng_free (r);
     
       return 0;
     }

Here is the output of the program,

     $ ./a.out
     0.99974
     0.16291
     0.28262
     0.94720
     0.23166
     0.48497
     0.95748
     0.74431
     0.54004
     0.73995

The numbers depend on the seed used by the generator. The default seed can be changed with the GSL_RNG_SEED environment variable to produce a different stream of numbers. The generator itself can be changed using the environment variable GSL_RNG_TYPE. Here is the output of the program using a seed value of 123 and the multiple-recursive generator mrg,

     $ GSL_RNG_SEED=123 GSL_RNG_TYPE=mrg ./a.out
     GSL_RNG_TYPE=mrg
     GSL_RNG_SEED=123
     0.33050
     0.86631
     0.32982
     0.67620
     0.53391
     0.06457
     0.16847
     0.70229
     0.04371
     0.86374