本文共 3565 字,大约阅读时间需要 11 分钟。
感谢博主:mishidemudong
地址:研究排序问题的时候常常需要生成随机数组来验证自己排序算法的正确性和性能,所以把Python生成随机数(组)的方法稍作总结,以备以后查看使用。
整数参数间的一个随机整数[low,hight) 1 random.randint(low,hight) 0-1之间随机浮点数左闭右开 2 random.random() 参数之间随机浮点数全闭合 3 random.uniform(val1,val2) 起点到终点按照间隔取得随机数 4 random.randrange(start, stop, step)>>> help(random.randint)Help on method randint in module random:randint(self, a, b) method of random.Random instanceReturn random integer in range [a, b], including both end points.
返回一个位于[low,hight]之间的整数。
该函数必须接受两个参数,这两个参数必须是整数(或者小数位是0的浮点数), 第一个整数参数必须小于等于第二个整数参数>>> random.randint(1, 10)6>>> random.randint(1.0, 10.0)8
>>> help(random.random)Help on built-in function random:random(...) random() -> x in the interval [0, 1).
不接受参数,返回一个[0.0, 1.0)之间的浮点数
>>> random.random()0.16638892353491153
>>> help(random.uniform)Help on method uniform in module random:uniform(self, a, b) method of random.Random instance Get a random number in the range [a, b) or [a, b] depending on rounding.
接受两个数字参数,返回两个数字区间的一个浮点数,不要求val1小于等于val2.
>>> random.uniform(1,4)3.5630633737872053
>>> help(random.randrange)Help on method randrange in module random:randrange(self, start, stop=None, step=1, _int=, _maxwidth=9007199254740992L) method of random.Random instance Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want.
返回以start开始,stop结束,step为步长的列表中的随机整数,同样,三个参数均为整数(或者小数位为0),
若start大于stop时 ,setp必须为负数 step不能是小数>>> random.randrange(1, 100, 2)17>>> random.randrange(100, 1, -2)28
import numpy as np
用于生成[0.0, 1.0)之间的随机浮点数。 np.random.rand(n) 用于生成[0.0, 1.0)之间的随机浮点数,但是具有正态分布特性。 np.random.randn(n) 返回随机的整数,位于半开区间 [low, high),size表示返回的个数。 np.random.randint(low[, high, size]) 返回随机的整数,位于闭合区间 [low, high],size表示返回的个数。 np.random_integers(low[, high, size]) 将序列中的元素进行随机顺序排列 np.random.shuffle(arr) 对arange(n)的arr进行位置随机排序,对比shuffle,直接产生arr np.random.permutation(n)用于生成[0.0, 1.0)之间的随机浮点数。
当没有参数时,返回一个随机浮点数。 当有一个参数时,返回该参数长度大小的一维随机浮点数数组,参数是整数型。>>> np.random.rand(3)array([ 0.46905771, 0.11792115, 0.38069103])>>> type(np.random.rand())>>> type(np.random.rand(3))
该函数返回具有标准正态一个样本。
>>> np.random.randn()-0.26784706346125065>>> np.random.randn(5)array([-1.2499142 , -0.06773846, -0.44777215, -1.55111136, 0.14573448])>>> type(np.random.rand())>>> type(np.random.rand(3))
返回随机的整数,位于半开区间 [low, high),且第一个参数默认是0可以省略。建议写出size参数关键字
>>> np.random.randint(2, 10) #因为没有写出size7>>> type(np.random.randint(0,2))>>> np.random.randint(2, size=10)array([0, 1, 0, 1, 0, 0, 0, 1, 1, 1])>>> np.random.randint(0,2, 10) # 所以不建议如此写array([1, 0, 1, 1, 0, 1, 0, 0, 0, 0])>>> np.random.randint(0,2, size=10)array([0, 0, 0, 1, 0, 0, 0, 0, 1, 1])# 初始化二维数组>>> np.random.randint(5, size=(2, 4))array([[4, 4, 4, 4], [3, 0, 2, 0]])
返回随机的整数,位于闭区间 [low, high]。
np.random.random_integers(0,2, size=10)
array([0, 1, 2, 0, 1, 1, 0, 0, 0, 1]) np.random.random_integers(5, size=(3,2)) array([[5, 4], [3, 3], [4, 5]])
类似洗牌,打乱顺序
>>> arr = np.arange(10)>>> arrarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> np.random.shuffle(arr)>>> arrarray([4, 2, 8, 1, 9, 7, 0, 6, 5, 3])
返回一个随机排列,相当于上面产生list的函数+shuffle函数的共同效果
>>> np.random.permutation(10)array([8, 5, 6, 7, 4, 0, 3, 1, 9, 2])
转载地址:http://roiqi.baihongyu.com/