隨機數的使用是很多算法的關鍵步驟,例如蒙特卡洛法、遺傳算法中的輪盤賭法的過程,因此對于任意一種語言,掌握其各類型隨機數生成的方法至關重要,Python與R在隨機數底層生成上都依靠梅森旋轉(twister)來生成高質量的隨機數,但在語法上存在著很多異同點。
Python
numpy中的random模塊
from numpy import random ?random Type: module String form: <module 'numpy.random' from 'D:\\anaconda\\lib\\site-packages\\numpy\\random\\__init__.py'> File: d:\anaconda\lib\site-packages\numpy\random\__init__.py Docstring: ======================== 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 random for i in range(10):print(random.random_sample()) 0.5131167122678871 0.3182844248720986 0.5391999374256481 0.2212549424277599 0.80648135792427 0.34225462561468434 0.5388888490671446 0.00587378555105833 0.6731524781805254 0.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 1
4.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
?