如何使用numpy库生成随机数组

阿里云2000元红包!本站用户参与享受九折优惠!

1. random.rand()方法

1.1 不传值

import numpy as np
a = np.random.rand()
print(a)
结果:
0.38263125170370416

说明:不传入参数,表示生成0到1之间的随机数

1.2 传入一个参数

import numpy as np
b = np.random.rand(10)
print(b)
结果:
[0.6015869  0.93864589 0.02830792 0.16461686 0.28153777 0.77099193
 0.11503517 0.92020664 0.55646181 0.04398042]

说明:传入一个参数,生成一个对应长度的且范围在0到1之间的随机数组

1.3 传入两个参数

import numpy as np
c = np.random.rand(20,20)
print(c)
结果:
[[0.46177325 0.66081961 0.21611182 0.56998479 0.36048155 0.58951269
  0.43576374 0.20117565 0.52007436 0.54222245 0.5874094  0.39413486
  0.92647082 0.95573729 0.12612035 0.01791213 0.55525058 0.04916815
  0.1059881  0.35420454]
 [0.741201   0.49306048 0.59388445 0.55307228 0.44777731 0.73903397
  0.59813579 0.58761902 0.72588363 0.83462968 0.88064133 0.49490255
  0.86148232 0.93813743 0.11198241 0.96166165 0.23885567 0.99723345
  0.98968899 0.47291647]
 [0.08487512 0.55998094 0.700673   0.06569818 0.40761038 0.45148651
  0.50759078 0.47847267 0.48956362 0.13906565 0.69303916 0.71070856
  0.26480507 0.80320914 0.05737245 0.38109918 0.49275737 0.6002883
  0.34752338 0.21737668]
 [0.17874898 0.25554796 0.30412177 0.00696131 0.07128795 0.81251756
  0.14047629 0.57610528 0.95764868 0.62450373 0.49022654 0.10043536
  0.09982361 0.5570304  0.98718292 0.32206512 0.30912603 0.19903629
  0.64054098 0.39754896]
 [0.35791213 0.0563493  0.33233007 0.53205464 0.70981406 0.71307109
  0.82525618 0.81770059 0.28942626 0.04750054 0.65662524 0.91821578
  0.82908504 0.05210041 0.46542977 0.92482103 0.70270805 0.2510357
  0.42442217 0.53342987]
 [0.40827472 0.25938767 0.96631441 0.25269866 0.54942489 0.74627352
  0.19014706 0.35668927 0.53228618 0.92269201 0.5635794  0.03609493
  0.60347754 0.35537515 0.31430264 0.75530457 0.0996403  0.75935296
  0.17143921 0.91255492]
 [0.23378766 0.41927299 0.07734925 0.95330274 0.09399703 0.07008424
  0.79116645 0.04997103 0.90631752 0.90503017 0.43922084 0.35924809
  0.8388717  0.98108365 0.50858744 0.89372408 0.41983387 0.3630741
  0.01655635 0.73194453]
 [0.78530595 0.41777663 0.18592803 0.46529468 0.42825055 0.24607134
  0.59895393 0.59885012 0.49154432 0.00245372 0.97420201 0.6437281
  0.1492626  0.39166019 0.95148036 0.40315853 0.91875232 0.25439232
  0.70572863 0.63365525]
 [0.67184986 0.93815191 0.17134469 0.12022747 0.87751775 0.58507113
  0.47778208 0.75299373 0.65311809 0.37514483 0.91766557 0.11479848
  0.43639861 0.19374344 0.51075777 0.42681065 0.1435665  0.48457134
  0.80223894 0.28562591]
 [0.77879158 0.63475788 0.92007785 0.0295611  0.36212806 0.46343699
  0.80975108 0.75521402 0.10161395 0.52237849 0.74907404 0.76311014
  0.13839892 0.04238508 0.93028081 0.57549331 0.09471343 0.06314428
  0.54766998 0.62641006]
 [0.15939104 0.0477785  0.89517006 0.86815574 0.71188229 0.77938613
  0.00305694 0.03050923 0.70055089 0.00570956 0.27841669 0.530724
  0.53004088 0.56084453 0.34633395 0.20202412 0.08131313 0.83937047
  0.93961987 0.82623405]
 [0.42433931 0.26718646 0.62680582 0.70922826 0.50115729 0.64327102
  0.57841009 0.22001071 0.53287125 0.70410177 0.8493939  0.1557961
  0.39364391 0.28727442 0.81819479 0.50549199 0.26150928 0.85087804
  0.54923194 0.86515693]
 [0.02170008 0.5056798  0.70641418 0.72285762 0.94813347 0.06232823
  0.32542045 0.80062519 0.35842956 0.11902905 0.80896527 0.68155977
  0.11100083 0.60182418 0.14195504 0.83109854 0.74300121 0.56633457
  0.5298753  0.12883176]
 [0.73638595 0.61326375 0.54700565 0.90201875 0.06170804 0.03718377
  0.53513277 0.50699015 0.99620034 0.35174463 0.37274472 0.08030701
  0.19799935 0.54710375 0.06197719 0.19053725 0.31704308 0.73264089
  0.899951   0.63465649]
 [0.83354046 0.61845305 0.94223124 0.69069164 0.10734682 0.27225985
  0.87780159 0.1646287  0.68236569 0.08202594 0.3202651  0.82313406
  0.94041592 0.43631087 0.86415853 0.00155323 0.56139983 0.87069205
  0.84154571 0.13128779]
 [0.83747416 0.44591603 0.09917223 0.32507025 0.26359933 0.19168354
  0.4638878  0.0240926  0.1335061  0.74732228 0.46756338 0.40452585
  0.16156038 0.16471332 0.28048331 0.32943559 0.77933974 0.66187369
  0.04256584 0.17319712]
 [0.32573348 0.01808569 0.96353591 0.18444417 0.43747955 0.50559683
  0.75769    0.57363002 0.90147932 0.05931192 0.34735985 0.25724545
  0.3088311  0.25929817 0.22395032 0.51982475 0.27607163 0.1615151
  0.09809688 0.06705644]
 [0.78698289 0.48242943 0.15517405 0.98599444 0.14647591 0.69377671
  0.30440766 0.23458629 0.52337381 0.80903418 0.28862557 0.63855826
  0.43511973 0.238779   0.991956   0.48320447 0.12779603 0.60034613
  0.42087241 0.95954316]
 [0.98537348 0.76216203 0.30621803 0.60910585 0.05441588 0.30951358
  0.21879278 0.24469593 0.04585956 0.49899933 0.15969888 0.11924618
  0.51836025 0.27036219 0.01056964 0.01683407 0.88324621 0.89982502
  0.95573504 0.29115035]
 [0.16248156 0.22526813 0.40146452 0.39393591 0.01810013 0.71885908
  0.02895894 0.53136449 0.18186804 0.37624181 0.90416146 0.51046335
  0.15255493 0.86292341 0.72062011 0.97397118 0.23726356 0.01079719
  0.90704517 0.70961783]]

说明:传入两个参数,生成对应行数和列数的多维数组,且元素的值范围0到1

https://www.jianshu.com/p/732ff7698eed

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论