np.random.randnp.random.randnnp.random.randint(n) 随机生成1个在区间[0, n)上的整数。import numpy as np
def show(title, m):
print(title.center(80, '-'))
print(m)
# 生成长度为5的1维[0, 1)随机数
m1 = np.random.rand(10)
show('m1', m1)
# 生成长度为10的1维[0, 1)随机数
m2 = np.random.rand(5, 3)
show('m2', m2)
# 生成长度为10的1维[0, 1)随机数
m3 = np.random.rand(5, 3, 2)
show('m3', m3)
---------------------------------------m1--------------------------------------- [0.833112 0.78366746 0.05900281 0.34861702 0.72713466 0.41009999 0.51358219 0.42456041 0.55495882 0.87558922] ---------------------------------------m2--------------------------------------- [[0.41791795 0.44918332 0.85505976] [0.88788453 0.69499783 0.10782279] [0.94694862 0.97632142 0.70799792] [0.00496677 0.75350443 0.1329044 ] [0.23262408 0.48361605 0.18461953]] ---------------------------------------m3--------------------------------------- [[[0.7919385 0.30971903] [0.09325939 0.62173986] [0.59168612 0.20398787]] [[0.31226192 0.34585393] [0.11248008 0.27677331] [0.54848741 0.3855126 ]] [[0.16854595 0.19853787] [0.49560004 0.92958179] [0.81672528 0.0256919 ]] [[0.29234142 0.8597146 ] [0.3044532 0.62233198] [0.95412626 0.95098013]] [[0.39828094 0.27563878] [0.51025127 0.55805215] [0.19946141 0.88882738]]]
import numpy as np
print(np.random.randint(10))
print(np.random.randint(10, 20))
print(np.random.randint(10, 20, 5))
[np.random.randint(10) for _ in range(10)]
4 12 [14 15 19 16 18]
[2, 9, 1, 2, 0, 7, 1, 6, 2, 6]
import numpy as np
def show(title, m):
print(title.center(80, '-'))
print(m)
show('m1', np.random.randn(5))
show('m2', np.random.randn(5,3))
show('m3', np.random.randn(5,3,2))
---------------------------------------m1--------------------------------------- [-1.9249834 0.41835021 0.73731119 0.52571419 -1.35116262] ---------------------------------------m2--------------------------------------- [[-0.40758071 -0.60590217 0.219938 ] [ 1.00542699 0.512375 -0.07398155] [-0.89750088 0.51217011 0.63057533] [-1.39735025 1.60483438 -0.64895009] [ 2.38018964 -1.01312136 0.93082658]] ---------------------------------------m3--------------------------------------- [[[ 1.76484326e+00 -1.56307230e+00] [-5.17361135e-01 3.62523635e-02] [-2.35162741e+00 4.73543709e-01]] [[ 6.12537709e-01 6.33039746e-01] [-2.39743648e-03 3.24696534e-01] [-6.91838399e-01 1.23777030e+00]] [[-6.14808092e-01 -4.28515314e-01] [ 2.71838291e-01 -9.12885649e-01] [ 7.98245318e-02 7.01403360e-01]] [[ 7.81848641e-03 -9.94547315e-01] [ 1.25075550e+00 1.89075834e+00] [-8.92651647e-01 -1.44987581e+00]] [[-1.37803795e-01 -5.34623558e-01] [ 1.43473075e+00 2.45286459e+00] [ 7.39434576e-01 -1.21579979e+00]]]
import numpy as np
print(np.random.uniform(10))
print(np.random.uniform(10, 20))
print(np.random.uniform(10, 20, 5))
5.4329078915583855 12.512163489239926 [13.19923322 17.08315624 12.17231536 19.15123562 16.37979071]
import numpy as np
def show(title, m):
print(title.center(80, '-'))
print(m)
show('1个数', np.array(5))
show('二维数组', np.array([5,6]))
show('数据类型', np.array([5,6], dtype='float32'))
--------------------------------------1个数--------------------------------------- 5 --------------------------------------二维数组-------------------------------------- [5 6] --------------------------------------数据类型-------------------------------------- [5. 6.]
import numpy as np
def show(title, m):
print(title.center(80, '-'))
print(m)
l1 = [1,2,3,4,5]
m1 = np.random.permutation(10)
m2 = np.random.permutation(l1)
m3 = np.random.shuffle(l1)
show('生成全排列', m1)
show('数组随机组合', m2)
show('数组随机组合', np.array(l1))
show('choice(m2)', np.random.choice(m2))
show('choice(l1)', np.random.choice(l1))
-------------------------------------生成全排列-------------------------------------- [8 7 9 5 6 3 1 4 0 2] -------------------------------------数组随机组合------------------------------------- [4 2 1 5 3] -------------------------------------数组随机组合------------------------------------- [4 1 2 3 5] -----------------------------------choice(m2)----------------------------------- 2 -----------------------------------choice(l1)----------------------------------- 2