随机整数:
import random
#生成0-99之间随机整数
print(random.randint(0,99))
随机浮点数:
>>> random.random()
0.5750683162290557
>>> import random
>>> random.uniform(1,10)
1.537218811475957
随机字符:
>>> random.choice('abcdefg$%^&*')
'$'
>>> random.choice('abcdefg$%^&*')
'c'
随机字符串:
>>> random.choice(['apple','peach','banana'])
'banana'
1) rand(d0,d1…dn) 根据d0-dn创建随机数数组,浮点数,[0,1),均匀分布
>>> import numpy as np
>>> a = np.random.rand(3,4,5)
>>> a
array([[[ 0.82052522, 0.39772757, 0.10702767, 0.8500243 , 0.0990338 ],
[ 0.41688268, 0.81852505, 0.37949649, 0.30477787, 0.89358199],
[ 0.29684884, 0.34452373, 0.80421074, 0.40758698, 0.33536057],
[ 0.73658926, 0.97701813, 0.57473218, 0.32008441, 0.3855367 ]],
[[ 0.94558899, 0.38385689, 0.86562463, 0.88437479, 0.65539631],
[ 0.33358347, 0.75279117, 0.20435261, 0.47775278, 0.26806775],
[ 0.76751492, 0.39889982, 0.55444728, 0.1457183 , 0.16721214],
[ 0.96924537, 0.96858455, 0.38874238, 0.68661647, 0.42201225]],
[[ 0.14854302, 0.9057184 , 0.32156218, 0.42248953, 0.51643453],
[ 0.67239167, 0.70776545, 0.13228446, 0.48603424, 0.57457654],
[ 0.92464257, 0.89040427, 0.54697102, 0.91909625, 0.64871797],
[ 0.05623798, 0.90024524, 0.2178354 , 0.02516971, 0.08885865]]])
2) randn(d0,d1…dn) 根据d0-dn创建随机数数组,标准正太分布
>>> sn = np.random.randn(3,4,5)
>>> sn
array([[[ 0.86337373, -1.28532967, 2.22071546, 0.57320661, 0.20942383],
[-0.45122977, 1.41654004, 0.40633233, -1.03517127, -1.00214081],
[-1.33099737, -0.10849854, -0.10623566, -1.40064714, 0.56515491],
[-1.95891391, 1.26105515, 1.84718306, -0.59877648, 0.14339642]],
[[-1.6822221 , -0.73728456, -0.61499899, -0.62996666, 0.07878297],
[-0.01023083, -1.13038369, -1.39895922, -0.53454577, 1.54093765],
[-0.57146229, -0.42142275, -1.10270702, -0.7692162 , -1.17711295],
[-0.19241645, -1.29446225, 0.9771069 , -2.82338869, -0.26035278]],
[[-0.25134972, 1.82536516, -0.28314402, -0.74347928, -0.05876521],
[-0.16721582, 0.93229841, -0.30961706, 0.50778018, -0.54840238],
[-0.72032043, 0.1800224 , 1.18602988, -1.42246267, -0.30880924],
[ 0.33634607, 1.7428904 , 0.15975399, 0.04358346, 0.74270667]]])
>>>
3) randint(low,[,high,shape]) 根据shape创建随机整数或整数数组,范围是 [low,high)
>>> b = np.random.randint(100,200,(3,4))
>>> b
array([[162, 101, 114, 108],
[133, 191, 150, 102],
[153, 150, 163, 176]])