Go to the first, previous, next, last section, table of contents.

Sampling from a random number generator

The following functions return uniformly distributed random numbers, either as integers or double precision floating point numbers. To obtain non-uniform distributions see section Random Number Distributions.

Random: unsigned long int gsl_rng_get (const gsl_rng * r)
This function returns a random integer from the generator r. The minimum and maximum values depend on the algorithm used, but all integers in the range [min,max] are equally likely. The values of min and max can determined using the auxiliary functions gsl_rng_max (r) and gsl_rng_min (r).

Random: double gsl_rng_uniform (const gsl_rng * r)
This function returns a double precision floating point number uniformly distributed in the range [0,1). The range includes 0.0 but excludes 1.0. The value is typically obtained by dividing the result of gsl_rng_get(r) by gsl_rng_max(r) + 1.0 in double precision. Some generators compute this ratio internally so that they can provide floating point numbers with more than 32 bits of randomness (the maximum number of bits that can be portably represented in a single unsigned long int).

Random: double gsl_rng_uniform_pos (const gsl_rng * r)
This function returns a positive double precision floating point number uniformly distributed in the range (0,1), excluding both 0.0 and 1.0. The number is obtained by sampling the generator with the algorithm of gsl_rng_uniform until a non-zero value is obtained. You can use this function if you need to avoid a singularity at 0.0.

Random: unsigned long int gsl_rng_uniform_int (const gsl_rng * r, unsigned long int n)
This function returns a random integer from 0 to n-1 inclusive. All integers in the range [0,n-1] are equally likely, regardless of the generator used. An offset correction is applied so that zero is always returned with the correct probability, for any minimum value of the underlying generator.

If n is larger than the range of the generator then the function calls the error handler with an error code of GSL_EINVAL and returns zero.


Go to the first, previous, next, last section, table of contents.