## File: Random-Number-Generator-Examples.html

package info (click to toggle)
gsl-ref-html 2.3-1
• links: PTS
• area: non-free
• in suites: bullseye, buster, sid
• size: 6,876 kB
• ctags: 4,574
• sloc: makefile: 35
 file content (146 lines) | stat: -rw-r--r-- 5,309 bytes parent folder | download
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 GNU Scientific Library – Reference Manual: Random Number Generator Examples

18.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
0.33050 0.86631 0.32982 0.67620 0.53391 0.06457 0.16847 0.70229 0.04371 0.86374