生成随机整型数值:
numpy.random.random_integers
(low, high=None, size=None)
返回介于 [low,high] 之间的整型数值。如果high缺省,返回值为[1,low]。size为返回值个数,如果缺省,返回个数为1。
>>> np.random.random_integers(5)
4
>>> type(np.random.random_integers(5))
<type 'int'>
>>> np.random.random_integers(5, size=(3,2))
array([[5, 4],
[3, 3],
[4, 5]])
numpy.random.
randint
(low, high=None, size=None, dtype='l')
返回介于 [low,high) 之间的整型数值。如果high缺省,返回值为[0,low]。size为返回值个数,如果缺省,返回个数为1。
>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
生成浮点型数值:
numpy.random.
rand
(d0, d1, ..., dn)
返回 [0,1) 之间的浮点型数值,如果参数缺省,返回单个浮点型数值。
>>> np.random.rand(3,2)
array([[ 0.14022471, 0.96360618],
[ 0.37601032, 0.25528411],
[ 0.49313049, 0.94909878]])
numpy.random.
RandomState()
生成任意精度的随机数,参数为生成随机数种子
random = numpy.random.RandomState(0)
for i in range(200):
a = random.uniform(-0.1, 0.1)
print (round(a, 2))