本文共 6836 字,大约阅读时间需要 22 分钟。
随机数的使用是很多算法的关键步骤,例如蒙特卡洛法、遗传算法中的轮盘赌法的过程,因此对于任意一种语言,掌握其各类型随机数生成的方法至关重要,Python与R在随机数底层生成上都依靠梅森旋转(twister)来生成高质量的随机数,但在语法上存在着很多异同点。
Python
numpy中的random模块
from numpy import random?randomType: moduleString form:File: d:\anaconda\lib\site-packages\numpy\random\__init__.pyDocstring: ========================Random Number Generation============================================ =========================================================Utility functions==============================================================================random_sample Uniformly distributed floats over ``[0, 1)``.random Alias for `random_sample`.bytes Uniformly distributed random bytes.random_integers Uniformly distributed integers in a given range.permutation Randomly permute a sequence / generate a random sequence.shuffle Randomly permute a sequence in place.seed Seed the random number generator.choice Random sample from 1-D array.==================== ============================================================================= =========================================================Compatibility functions==============================================================================rand Uniformly distributed values.randn Normally distributed values.ranf Uniformly distributed floating point numbers.randint Uniformly distributed integers in a given range.==================== ============================================================================= =========================================================Univariate distributions==============================================================================beta Beta distribution over ``[0, 1]``.binomial Binomial distribution.chisquare :math:`\chi^2` distribution.exponential Exponential distribution.f F (Fisher-Snedecor) distribution.gamma Gamma distribution.geometric Geometric distribution.gumbel Gumbel distribution.hypergeometric Hypergeometric distribution.laplace Laplace distribution.logistic Logistic distribution.lognormal Log-normal distribution.logseries Logarithmic series distribution.negative_binomial Negative binomial distribution.noncentral_chisquare Non-central chi-square distribution.noncentral_f Non-central F distribution.normal Normal / Gaussian distribution.pareto Pareto distribution.poisson Poisson distribution.power Power distribution.rayleigh Rayleigh distribution.triangular Triangular distribution.uniform Uniform distribution.vonmises Von Mises circular distribution.wald Wald (inverse Gaussian) distribution.weibull Weibull distribution.zipf Zipf's distribution over ranked data.==================== ============================================================================= =========================================================Multivariate distributions==============================================================================dirichlet Multivariate generalization of Beta distribution.multinomial Multivariate generalization of the binomial distribution.multivariate_normal Multivariate generalization of the normal distribution.==================== ============================================================================= =========================================================Standard distributions==============================================================================standard_cauchy Standard Cauchy-Lorentz distribution.standard_exponential Standard exponential distribution.standard_gamma Standard Gamma distribution.standard_normal Standard normal distribution.standard_t Standard Student's t-distribution.==================== ============================================================================= =========================================================Internal functions==============================================================================get_state Get tuple representing internal state of generator.set_state Set state of generator.==================== =========================================================
上述random的模块说明文档详细说明了random中内置的各种随机数生成方法,下面针对其中一些常见的举例说明:
1.random.random_sample()与random.random()
生成[0,1]之间的服从均匀分布的浮点随机数
from numpy import randomfor i in range(10): print(random.random_sample())0.51311671226788710.31828442487209860.53919993742564810.22125494242775990.806481357924270.342254625614684340.53888884906714460.005873785551058330.67315247818052540.21002426217873815
2.random.random_integers()
生成指定范围内的可重复整数
random.random_integers(1,10,10)Out[44]: array([ 9, 10, 6, 4, 10, 10, 5, 3, 1, 6])
3.random.permutation()
生成指定范围内所有整数的一次随机排列
for i in range(5): token = random.permutation(5) print(token) print(set(token))[0 2 1 3 4]{0, 1, 2, 3, 4}[0 3 4 2 1]{0, 1, 2, 3, 4}[2 3 1 4 0]{0, 1, 2, 3, 4}[4 3 0 1 2]{0, 1, 2, 3, 4}[1 2 4 0 3]{0, 1, 2, 3, 4}
4.random.shuffle()
将指定的列表随机打乱顺序
list = [i for i in range(10)]random.shuffle(list)print(list)[6, 8, 2, 4, 5, 3, 0, 7, 1, 9]
5.random.seed()
以括号中的整数为起点设置伪随机数种子,同样的随机数种子设置后生成的随机数相同
random.seed(42)print(random.permutation(5))random.seed(42)print(random.permutation(5))[1 4 2 0 3][1 4 2 0 3]
6.random.choice()
从制定的序列中随机抽取多个元素(有放回或无放回,通过replace参数控制)
list = [i for i in range(10)]random.choice(list,6,replace=False)#有放回Out[8]: array([9, 6, 4, 2, 7, 8])random.choice(list,6,replace=False)#无放回Out[9]: array([1, 3, 9, 4, 0, 8])
7.random.rand()
生成0-1中服从均匀分布的多个随机数
random.rand(5)Out[19]: array([0.86317047, 0.43070734, 0.85228662, 0.74797087, 0.76224563])
8.random.randn()
生成多个服从标准正态分布的随机数
random.randn(10)Out[21]: array([-0.25617082, -0.85531159, -0.18286371, 1.25656827, -0.72270841, 0.13949334, 0.92318096, -1.12549131, -0.46908035, -0.28388281])
9.random.randint()
等可能的生成指定范围内的多个随机整数
random.randint(1,10,5)Out[29]: array([2, 9, 8, 8, 9])
R
作为专为统计而生的一种语言,R在随机数生成上自然是异常的丰富,这里仅举常用的一些随机数生成函数
1.rnorm()
生成服从正态分布的随机数,其中参数mean控制均值,sd控制标准差
> rnorm(5,mean=0,sd=1)[1] -0.36167951 -0.50435239 -0.20245800 0.07877604 0.23662553
2.runif()
生成指定范围内的均匀分布随机数
> runif(5, min=0,max=10)[1] 3.2774081 1.7341489 8.4128022 3.1511841 0.3385417
3.sample()
以不放回的方式生成指定范围内的随机整数序列
> sample(1:10,5,replace=T)#有放回
[1] 4 9 3 4 4> sample(1:10,5,replace=F)#无放回[1] 3 2 6 8 14.set.seed()
以括号内的整数值作为随机数发生算法的起点,因此通过控制伪随机数种子的参数,可以实现随机抽样的重现
而真正的随机算法里是默认以系统时间等我们认为充分随机的数字作为起点
> set.seed(42)> sample(1:10,5,replace=F)[1] 10 9 3 6 4> set.seed(42)> sample(1:10,5,replace=F)[1] 10 9 3 6 4
转载地址:http://qacuz.baihongyu.com/