GSL(GNU Scientific Library)参考手册/随机数发生器/生成随机数
生成随机数
编辑下面的函数返回整型或双精度浮点型均匀分布的随机数。定义HAVE_INLINE时将使用这些函数的内联(Inline)版本。要得到非均匀分布,请见随机数分布
函数:
unsigned long int gsl_rng_get (const gsl_rng * r)
此函数从生成器r返回一个随机整数。最大值和最小值取决于所使用的算法,但[min, max]之间的所有整数出现的概率是相等的。最大值max和最小值min可以通过辅助函数gsl_rng_max(r)和gsl_rng_min(r)来设置。
函数:
double gsl_rng_uniform (const gsl_rng * r)
此函数随机返回一个[0,1)中的双精度浮点数,范围包括0.0,但不包括1.0。此函数的返回值通常是用gsl_rng_get(r)的返回值除以gsl_rng_max(r)+1.0得到的。 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).
函数:
double gsl_rng_uniform_pos (const gsl_rng * r)
此函数随机返回一个[0,1)中的双精度浮点数,范围不包括0.0和1.0。该随机数是采用gsl_rng_uniform的算法抽样并舍去等于0的结果而得到的。此函数可用来避免0引起的奇异性。
函数:
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_EINVAL and 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 minimum value, such as gsl_rng_ranlxd1, gsl_rng_mt19937 or gsl_rng_taus, and sample it directly using gsl_rng_get. The range of each generator can be found using the auxiliary functions described in the next section.