随机创建数组_1

  • 语法格式
numpy.random.random(size=None)
  • numpy.random.randint()
该方法有三个参数 low、high、size 三个参数。默认 high 是 None,如果只有 low,那范围就是[0,low)。如果有 high,范围就是[low,high)。
  • 代码

 1 #导入模块
 2 import numpy as np 
 3 def randomTest():
 4     #使用random创建一维数组
 5     a = np.random.random(size = 5)
 6     print(a)
 7     print(type(a))
 8     print("************************************************")
 9 
10     #创建二维数组
11     b = np.random.random(size = (3,4))
12     print(b)
13     print("************************************************")
14 
15     #创建三维数组
16     c = np.random.random(size = (2,3,4))
17     print(c)
18     print("************************************************")
19 
20 #随机整数
21 def randomintTest():
22     #生成0-5之间的随机整数(一维)
23     a = np.random.randint(6,size=10)
24     print(a)
25     print(type(a))
26     print("************************************************")
27 
28     #生成5-10之间的随机整数
29     b = np.random.randint(5,11,size=(4,3))
30     print(b)
31     print("************************************************")
32 
33     #生成5-10之间的随机数(三维)
34     c = np.random.randint(5,11,size=(2,4,3))
35     print(c)
36     print("************************************************")
37 
38 #调用
39 randomTest()
40 randomintTest()
 1 [0.58481035 0.89981199 0.4430777  0.79111114 0.80530942]
 2 <class 'numpy.ndarray'>
 3 ************************************************
 4 [[0.62548615 0.30478776 0.80835854 0.64659382]  
 5  [0.01354597 0.55416311 0.28586102 0.13735663]  
 6  [0.51881331 0.21160145 0.72725684 0.99663842]] 
 7 ************************************************
 8 [[[0.08170694 0.9253817  0.65025442 0.04592365] 
 9   [0.36387681 0.65987097 0.10837108 0.15505963]
10   [0.77084731 0.00175051 0.44813178 0.82772988]]
11 
12  [[0.73729773 0.65277105 0.68823734 0.0330837 ]
13   [0.32585801 0.72776357 0.90520657 0.69459254]
14   [0.30655539 0.72554569 0.73852885 0.80749657]]]
15 ************************************************
16 [3 4 4 1 4 2 4 5 4 0]
17 <class 'numpy.ndarray'>
18 ************************************************
19 [[ 5  6  9]
20  [10 10  5]
21  [ 9  6  6]
22  [ 9  8  6]]
23 ************************************************
24 [[[ 7  6  7]
25   [ 9  6 10]
26   [ 8  7 10]
27   [10  9  7]]
28 
29  [[ 7  6  5]
30   [ 9 10  8]
31   [ 6  7  7]
32   [ 9  6  8]]]
33 ************************************************

猜你喜欢

转载自www.cnblogs.com/monsterhy123/p/12584554.html