| GNU Scientific Library Reference Manual - Third Edition (v1.12) by M. Galassi, J. Davies, J. Theiler, B. Gough, G. Jungman, P. Alken, M. Booth, F. Rossi Paperback (6"x9"), 592 pages, 60 figures ISBN 0954612078 RRP £24.95 ($39.95) |
17.4 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 19.
- Function: 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)andgsl_rng_min (r).
- Function: 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)bygsl_rng_max(r) + 1.0in 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 singleunsigned long int).
- Function: 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_uniformuntil a non-zero value is obtained. You can use this function if you need to avoid a singularity at 0.0.
- Function: 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
by scaling down and/or discarding samples from the generator r.
All integers in the range [0,n-1] are produced with equal
probability. For generators with a non-zero minimum value an offset
is applied so that zero is returned with the correct probability.
Note that this function is designed for sampling from ranges smaller than the range of the underlying generator. The parameter n must be less than or equal to the range of the generator r. If n is larger than the range of the generator then the function calls the error handler with an error code of
GSL_EINVALand returns zero.In particular, this function is not intended for generating the full range of unsigned integer values [0,2^32-1]. Instead choose a generator with the maximal integer range and zero mimimum value, such as
gsl_rng_ranlxd1,gsl_rng_mt19937orgsl_rng_taus, and sample it directly usinggsl_rng_get. The range of each generator can be found using the auxiliary functions described in the next section.
| ISBN 0954612078 | GNU Scientific Library Reference Manual - Third Edition (v1.12) | See the print edition |