{"id":170854,"date":"2023-08-04T18:33:36","date_gmt":"2023-08-04T10:33:36","guid":{"rendered":"http:\/\/www.idc.net\/help\/170854\/"},"modified":"2023-08-04T18:33:36","modified_gmt":"2023-08-04T10:33:36","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e5%a6%82%e4%bd%95%e7%94%9f%e4%ba%a7%e9%9a%8f%e6%9c%ba%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/170854\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython\u5982\u4f55\u751f\u4ea7\u968f\u673a\u6570\u5b57"},"content":{"rendered":"<blockquote>\n<p>python\u751f\u4ea7\u968f\u673a\u6570\u5b57\u7684\u65b9\u6cd5\u662f\uff1a1\u3001\u5229\u7528random\u5e93\u7684randint\u51fd\u6570\u751f\u6210\u968f\u673a\u6574\u6570\u3001uniform\u51fd\u6570\u968f\u673a\u751f\u6210\u6d6e\u70b9\u6570\u7b49\uff1b2\u3001\u5229\u7528\u7b2c\u4e09\u65b9\u5e93numpy\u4e2drandn\u51fd\u6570\u751f\u6210\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u77e9\u9635\u3002<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/016\/5f052d147e0d4596.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5728Python\u4e2d\u53ef\u4ee5\u7528\u4e8e\u968f\u673a\u6570\u751f\u6210\u7684\u6709\u4e24\u79cd\u4e3b\u8981\u9014\u5f84\uff0c\u4e00\u662frandom\u6a21\u5757\uff0c\u53e6\u4e00\u4e2a\u662fnumpy\u5e93\u4e2drandom\u51fd\u6570\u3002<\/p>\n<p style=\"margin: 8px 0px 16px;padding: 0px;, sans-serif;font-size: 24px;line-height: 32px\"><span style=\"font-size: 18px\"><strong>OUTLINE<\/strong><\/span><\/p>\n<ul>\n<li>\n<p>random\u6a21\u5757<\/p>\n<\/li>\n<li>\n<p>numpy\u4e2d\u7684random\u51fd\u6570<\/p>\n<\/li>\n<li>\n<p>\u603b\u7ed3<\/p>\n<\/li>\n<\/ul>\n<p style=\"margin: 8px 0px 16px;padding: 0px;, sans-serif;font-size: 24px;line-height: 32px\"><span style=\"font-size: 18px\"><strong>random\u6a21\u5757<\/strong><\/span><\/p>\n<p>random\u6a21\u5757\u4e2d\u5c06\u8fd1\u67097\u4e2a\u51fd\u6570\u90fd\u662f\u53ef\u4ee5\u7528\u6765\u751f\u6210\u968f\u673a\u6570\u7684\uff1a<\/p>\n<p>\u2460 random.random()<br \/><strong>\u529f\u80fd<\/strong>\uff1a\u968f\u673a\u751f\u6210\u4e00\u4e2a&nbsp;[0,1)&nbsp;\u7684\u6d6e\u70b9\u6570<br \/><strong>\u7528\u6cd5<\/strong>\uff1a<\/p>\n<pre>import&nbsp;random\nnumber&nbsp;=&nbsp;random.random()\n#&nbsp;\u8f93\u51fa\uff1a0.7178886135325596<\/pre>\n<p>\u2461 random.uniform(a,b)<br \/><strong>\u529f\u80fd<\/strong>\uff1a\u968f\u673a\u751f\u6210\u4e00\u4e2a&nbsp;[a,b)&nbsp;\u7684\u6d6e\u70b9\u6570<br \/><strong>\u7528\u6cd5<\/strong>\uff1a<\/p>\n<pre>number&nbsp;=&nbsp;random.uniform(2,4)\n#&nbsp;\u8f93\u51fa\uff1a2.070517705689751<\/pre>\n<p>\u2462 random.randint(a,b)<br \/><strong>\u529f\u80fd\uff1a<\/strong>\u968f\u673a\u751f\u6210\u4e00\u4e2a [a,b] \u7684\u6574\u6570<br \/><strong>\u7528\u6cd5<\/strong>\uff1a<\/p>\n<pre>number&nbsp;=&nbsp;random.randint(2,4)\n#&nbsp;\u8f93\u51fa\uff1a3<\/pre>\n<p>\u2463 random.randrange(a,b,step)<br \/><strong>\u529f\u80fd\uff1a<\/strong>\u5728\u751f\u6210\u7684&lt;\u4ee5a\u4e3a\u59cb\uff0c\u6bcfstep\u9012\u589e\uff0c\u4ee5b\u4e3a\u7ec8&gt;\u8fd9\u6837\u7684\u4e00\u4e2a\u6574\u6570\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u6570<br \/><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>number&nbsp;=&nbsp;random.randrange(2,10,2)\n#&nbsp;\u8f93\u51fa\uff1a2<\/pre>\n<p><strong><\/strong>\u2464 random.choice(sequence)<br \/><strong>\u529f\u80fd\uff1a<\/strong>\u4ece\u4e00\u4e2a\u5df2\u6709\u7684sequence\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20<br \/><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>number&nbsp;=&nbsp;random.choice(range(2,10))\n#&nbsp;\u8f93\u51fa\uff1a6\nnumber&nbsp;=&nbsp;random.choice('anbegvdje')\n#&nbsp;\u8f93\u51fa\uff1ag<\/pre>\n<p>\u2465 random.sample(sequence,k)<br \/><strong>\u529f\u80fd\uff1a<\/strong>\u4ece\u67d0\u4e00\u5e8f\u5217\u4e2d\u83b7\u53d6\u6307\u5b9a\u957f\u5ea6\u7684\u7247\u6bb5<br \/><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>a&nbsp;=&nbsp;['\u9999\u8549','\u82f9\u679c','\u6a58\u5b50','\u773c\u5f71','\u773c\u7ebf']\nb&nbsp;=&nbsp;random.sample(a,2)#&nbsp;\u8f93\u51fa\uff1a['\u6a58\u5b50',&nbsp;'\u82f9\u679c']<\/pre>\n<p>\u2466 random.shuffle(\u5217\u8868)<br \/><strong>\u529f\u80fd\uff1a<\/strong>\u6253\u4e71\u4e00\u4e2a\u5217\u8868\u7684\u5143\u7d20\u987a\u5e8f<br \/><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>a&nbsp;=&nbsp;['\u9999\u8549','\u82f9\u679c','\u6a58\u5b50','\u773c\u5f71','\u773c\u7ebf']\nrandom.shuffle(a)&nbsp;#&nbsp;\u6253\u4e71\u8fd9\u4e2a\u64cd\u4f5c\u4e0d\u9700\u8981\u91cd\u65b0\u5b9a\u4e49\u5bf9\u8c61\uff0c\u5b83\u4f5c\u7528\u7684\u662f\u5bf9\u8c61\u672c\u8eab\n#&nbsp;\u8f93\u51fa\uff1a['\u773c\u7ebf',&nbsp;'\u82f9\u679c',&nbsp;'\u773c\u5f71',&nbsp;'\u6a58\u5b50',&nbsp;'\u9999\u8549']<\/pre>\n<p><strong><\/strong><\/p>\n<p style=\"margin: 8px 0px 16px;padding: 0px;, sans-serif;font-size: 24px;line-height: 32px\"><strong><span style=\"font-size: 18px\">numpy\u4e2d\u7684random\u51fd\u6570<\/span><\/strong><\/p>\n<p>numpy\u4e2d\u7684random\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u4e8c\u662f\u751f\u6210\u968f\u673a\u6574\u6570\u3002<\/p>\n<p>\u2460 np.random.randn(a,b)<br \/><strong>\u529f\u80fd\uff1a<\/strong>\u751f\u6210a*b\u7ef4\u7684\u968f\u673a\u6570\uff0c\u4e14\u8be5\u6570\u670d\u4ece\u6807\u51c6\u6b63\u592a\u5206\u5e03<br \/><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>data&nbsp;=&nbsp;np.random.randn(5,4)\n#&nbsp;\u8f93\u51fa\uff1a\narray([[-1.6101468&nbsp;,&nbsp;-0.81103612,&nbsp;&nbsp;0.44875047,&nbsp;&nbsp;0.55987574],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-0.33322916,&nbsp;&nbsp;0.18676658,&nbsp;-0.18424432,&nbsp;-0.84435811],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;0.57654276,&nbsp;&nbsp;0.28830858,&nbsp;-0.73403656,&nbsp;&nbsp;1.59404864],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;0.39009202,&nbsp;&nbsp;0.86239796,&nbsp;&nbsp;0.66290243,&nbsp;-0.61292579],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;0.03081516,&nbsp;&nbsp;0.99335315,&nbsp;-0.6875357&nbsp;,&nbsp;&nbsp;0.90552971]])<\/pre>\n<p>\u2461 random.randint(low,high,size)<br \/><strong>\u529f\u80fd\uff1a<\/strong>\u751f\u6210\u4e00\u4e2a&lt;\u4ee5low\u4e3a\u4e0b\u9650\uff0chigh\u4e3a\u4e0a\u9650\uff0csize\u5927\u5c0f&gt;\u7684\u968f\u673a\u6574\u6570\u77e9\u9635\uff0c\u5176\u4e2d\u6570\u503c\u8303\u56f4\u5305\u542blow\uff0c\u4e0d\u5305\u542bhigh<br \/><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>data&nbsp;=&nbsp;np.random.randint(low=2,high=5,size=(5,7))\n#&nbsp;\u8f93\u51fa:\narray([[4,&nbsp;2,&nbsp;4,&nbsp;4,&nbsp;4,&nbsp;4,&nbsp;2],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[4,&nbsp;2,&nbsp;2,&nbsp;4,&nbsp;3,&nbsp;3,&nbsp;3],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3,&nbsp;4,&nbsp;3,&nbsp;4,&nbsp;3,&nbsp;3,&nbsp;4],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3,&nbsp;4,&nbsp;2,&nbsp;3,&nbsp;3,&nbsp;2,&nbsp;2],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3,&nbsp;3,&nbsp;3,&nbsp;3,&nbsp;2,&nbsp;3,&nbsp;2]])<\/pre>\n<p style=\"margin: 8px 0px 16px;padding: 0px;, sans-serif;font-size: 24px;line-height: 32px\"><strong><span style=\"font-size: 18px\">\u603b\u7ed3<\/span><\/strong><\/p>\n<p>1\u3001\u5728\u6211\u4eec\u65e5\u5e38\u4f7f\u7528\u4e2d\uff0c\u5982\u679c\u662f\u4e3a\u4e86\u5f97\u5230\u968f\u673a\u7684\u5355\u4e2a\u6570\uff0c\u591a\u8003\u8651random\u6a21\u5757\uff1b\u5982\u679c\u662f\u4e3a\u4e86\u5f97\u5230\u968f\u673a\u5c0f\u6570\u6216\u8005\u6574\u6570\u7684\u77e9\u9635\uff0c\u5c31\u591a\u8003\u8651numpy\u4e2d\u7684random\u51fd\u6570\uff1b<\/p>\n<p>2\u3001\u5bf9\u4e8erandom\u6a21\u5757\u7684\u51fd\u6570\u8c03\u7528\u65b9\u6cd5\u7684\u8bb0\u5fc6\uff0c\u53ef\u4ee5\u591a\u4ece\u5b83\u672c\u8eab\u7684\u82f1\u8bd1\u51fa\u53d1\uff0c\u5e76\u591a\u6bd4\u8f83\u5176\u4e0d\u540c\u4ece\u800c\u52a0\u6df1\u7406\u89e3~<\/p>\n<blockquote>\n<p>\u63a8\u8350\u8bfe\u7a0b\uff1aIntroduction to Even More Python for Beginners\uff08\u5fae\u8f6f\u5b98\u65b9\u8bfe\u7a0b\uff09<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>python\u751f\u4ea7\u968f\u673a\u6570\u5b57\u7684\u65b9\u6cd5\u662f\uff1a1\u3001\u5229\u7528random\u5e93\u7684randint\u51fd\u6570\u751f\u6210\u968f\u673a\u6574\u6570\u3001uniform\u51fd\u6570\u968f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":170855,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-170854","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/170854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=170854"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/170854\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/170855"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=170854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=170854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=170854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}