{"id":176569,"date":"2024-05-08T19:13:26","date_gmt":"2024-05-08T11:13:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176569.html"},"modified":"2024-05-08T19:13:33","modified_gmt":"2024-05-08T11:13:33","slug":"python%e6%80%8e%e4%b9%88%e6%8c%89%e7%85%a7%e7%89%b9%e5%ae%9a%e5%88%86%e5%b8%83%e7%94%9f%e6%88%90%e9%9a%8f%e6%9c%ba%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/176569.html","title":{"rendered":"python\u600e\u4e48\u6309\u7167\u7279\u5b9a\u5206\u5e03\u751f\u6210\u968f\u673a\u6570"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060233\/7b9bf111-f505-43a6-b7b7-42bd9892e7f2.webp\" alt=\"python\u600e\u4e48\u6309\u7167\u7279\u5b9a\u5206\u5e03\u751f\u6210\u968f\u673a\u6570\" \/><\/p>\n<p><p>\u5728Python\u4e2d\u6309\u7167\u7279\u5b9a\u5206\u5e03\u751f\u6210\u968f\u673a\u6570\u4e3b\u8981\u4f9d\u9760\u6807\u51c6\u5e93<code>random<\/code>\u548c\u7b2c\u4e09\u65b9\u5e93\u5982<code>NumPy<\/code>\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>random<\/code>\u5e93\u7684\u51fd\u6570\u5bf9\u4e8e\u57fa\u672c\u5206\u5e03\u3001\u4f7f\u7528<code>NumPy<\/code>\u5e93\u751f\u6210\u9ad8\u7ea7\u5206\u5e03\u7684\u968f\u673a\u6570<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<code>NumPy<\/code>\u7684\u529f\u80fd\u5c24\u4e3a\u5f3a\u5927\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u751f\u6210\u5404\u79cd\u590d\u6742\u5206\u5e03\u7684\u968f\u673a\u6570\u3002\u4f8b\u5982\uff0c\u8981\u751f\u6210\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random.normal()<\/code>\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u6307\u5b9a\u671f\u671b\u503c\u548c\u6807\u51c6\u5dee\uff0c\u4ece\u800c\u751f\u6210\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u636e\u3002\u8fd9\u5bf9\u4e8e\u6a21\u62df\u5b9e\u9a8c\u6570\u636e\u3001\u8fdb\u884c\u7edf\u8ba1\u6d4b\u8bd5\u7b49\u573a\u666f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h2><strong>\u4e00\u3001\u4f7f\u7528<code>random<\/code>\u5e93\u751f\u6210\u968f\u673a\u6570<\/strong><\/h2>\n<\/p>\n<p><p><code>random<\/code>\u5e93\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u636e\u7684\u57fa\u672c\u529f\u80fd\u3002\u5c3d\u7ba1\u5b83\u4e0d\u50cf<code>NumPy<\/code>\u90a3\u6837\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u590d\u6742\u7684\u5206\u5e03\uff0c\u4f46\u5bf9\u4e8e\u4e00\u4e9b\u7b80\u5355\u9700\u6c42\uff0c<code>random<\/code>\u5e93\u5df2\u7ecf\u8db3\u591f\u7528\u4e86\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>random<\/code>\u5e93\u53ef\u4ee5\u751f\u6210\u57fa\u672c\u7684\u968f\u673a\u6570\u636e\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\u3002\u4f8b\u5982\uff0c<code>random.randint(a, b)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5728[a,b]\u8303\u56f4\u5185\u7684\u6574\u6570\uff0c\u800c<code>random.uniform(a, b)<\/code>\u5219\u751f\u6210\u4e00\u4e2a\u5728[a,b]\u8303\u56f4\u5185\u7684\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u5206\u5e03<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u65e0\u6cd5\u76f4\u63a5\u901a\u8fc7\u51fd\u6570\u751f\u6210\u7684\u5206\u5e03\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ec4\u5408\u4f7f\u7528<code>random<\/code>\u5e93\u4e2d\u7684\u57fa\u672c\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u6bd4\u5982\uff0c\u5229\u7528<code>random.random()<\/code>\u751f\u6210[0,1)\u533a\u95f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u518d\u901a\u8fc7\u9002\u5f53\u7684\u6570\u5b66\u53d8\u6362\u5f97\u5230\u7b26\u5408\u7279\u5b9a\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2><strong>\u4e8c\u3001\u4f7f\u7528<code>NumPy<\/code>\u5e93\u751f\u6210\u590d\u6742\u5206\u5e03\u7684\u968f\u673a\u6570<\/strong><\/h2>\n<\/p>\n<p><p><code>NumPy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5176\u968f\u673a\u6a21\u5757<code>numpy.random<\/code>\u63d0\u4f9b\u4e86\u66f4\u52a0\u4e30\u5bcc\u7684\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u7279\u5b9a\u6982\u7387\u5206\u5e03\u7684\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\u975e\u5e38\u5168\u9762\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u6b63\u6001\u5206\u5e03<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.random.normal(loc=0.0, scale=1.0, size=None)<\/code>\u53ef\u4ee5\u751f\u6210\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u6570\u7ec4\uff0c\u5176\u4e2d<code>loc<\/code>\u3001<code>scale<\/code>\u548c<code>size<\/code>\u5206\u522b\u5bf9\u5e94\u4e8e\u5206\u5e03\u7684\u5747\u503c\u3001\u6807\u51c6\u5dee\u548c\u8f93\u51fa\u7684\u5f62\u72b6\uff08\u6216\u5927\u5c0f\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5747\u5300\u5206\u5e03<\/strong><\/p>\n<\/p>\n<p><p><code>numpy.random.uniform(low=0.0, high=1.0, size=None)<\/code>\u5141\u8bb8\u751f\u6210\u5728\u6307\u5b9a\u8303\u56f4\u5185\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u5728\u7279\u5b9a\u533a\u95f4\u5185\u5747\u5300\u62bd\u53d6\u6837\u672c\u7684\u60c5\u5f62\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2><strong>\u4e09\u3001\u5176\u4ed6\u5206\u5e03\u7684\u968f\u673a\u6570\u751f\u6210<\/strong><\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u5e38\u89c1\u7684\u6b63\u6001\u5206\u5e03\u548c\u5747\u5300\u5206\u5e03\u5916\uff0c<code>NumPy<\/code>\u8fd8\u652f\u6301\u5305\u62ec\u4e8c\u9879\u5206\u5e03\u3001\u6cca\u677e\u5206\u5e03\u3001\u6307\u6570\u5206\u5e03\u7b49\u5728\u5185\u7684\u591a\u79cd\u6982\u7387\u5206\u5e03\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4e8c\u9879\u5206\u5e03<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u9879\u5206\u5e03\u968f\u673a\u6570\u53ef\u901a\u8fc7<code>numpy.random.binomial(n, p, size=None)<\/code>\u751f\u6210\uff0c\u5176\u4e2d<code>n<\/code>\u662f\u8bd5\u9a8c\u6b21\u6570\uff0c<code>p<\/code>\u662f\u6bcf\u6b21\u8bd5\u9a8c\u6210\u529f\u7684\u6982\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6307\u6570\u5206\u5e03<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>numpy.random.exponential(scale=1.0, size=None)<\/code>\u53ef\u4ee5\u751f\u6210\u7b26\u5408\u6307\u6570\u5206\u5e03\u7684\u968f\u673a\u6570\uff0c\u5176\u4e2d<code>scale<\/code>\u662f\u5206\u5e03\u7684\u6bd4\u4f8b\u53c2\u6570\uff08\u53731\/\u03bb\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2><strong>\u56db\u3001\u9ad8\u7ea7\u5e94\u7528<\/strong><\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u7840\u7684\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\uff0c\u4e86\u89e3\u5982\u4f55\u6839\u636e\u7279\u5b9a\u9700\u6c42\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5bf9\u4e8e\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6a21\u578b\u548c\u6a21\u62df\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u62bd\u6837\u4e0e\u6a21\u62df<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u6982\u7387\u6a21\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>NumPy<\/code>\u7684\u968f\u673a\u6570\u751f\u6210\u51fd\u6570\u8fdb\u884c\u62bd\u6837\u6a21\u62df\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u91d1\u878d\u3001\u7269\u7406\u7b49\u9886\u57df\u7684\u6a21\u578b\u9884\u6d4b\u4e2d\u5c24\u4e3a\u5e38\u89c1\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u968f\u673a\u6570\u7684\u79cd\u5b50<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u968f\u673a\u6570\u751f\u6210\u65f6\uff0c\u8bbe\u7f6e\u968f\u673a\u79cd\u5b50\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\uff0c\u5b83\u80fd\u591f\u4fdd\u8bc1\u968f\u673a\u6570\u5e8f\u5217\u7684\u53ef\u91cd\u590d\u6027\u3002\u5728<code>random<\/code>\u548c<code>NumPy<\/code>\u4e2d\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u79cd\u5b50\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u5982<code>random.seed()<\/code>\u6216<code>numpy.random.seed()<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u603b\u4e4b\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65b9\u6cd5\u548c\u5de5\u5177\u6765\u6309\u7279\u5b9a\u5206\u5e03\u751f\u6210\u968f\u673a\u6570\uff0c\u65e0\u8bba\u662f\u901a\u8fc7\u7b80\u5355\u7684<code>random<\/code>\u5e93\u8fd8\u662f\u529f\u80fd\u66f4\u5f3a\u5927\u7684<code>NumPy<\/code>\u5e93\uff0c\u90fd\u80fd\u6ee1\u8db3\u4e0d\u540c\u9886\u57df\u548c\u9700\u6c42\u4e0b\u7684\u968f\u673a\u6570\u751f\u6210\u3002\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u3001\u79d1\u5b66\u8ba1\u7b97\u4e43\u81f3<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u9886\u57df\u7684\u7814\u7a76\u548c\u5e94\u7528\u90fd\u5177\u6709\u91cd\u8981\u610f\u4e49\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1. \u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\uff1f<\/strong><\/p>\n<p>\u6b63\u6001\u5206\u5e03\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6982\u7387\u5206\u5e03\uff0c\u53ef\u4ee5\u7528\u4e8e\u6a21\u62df\u5b9e\u9645\u4e16\u754c\u4e2d\u7684\u8bb8\u591a\u968f\u673a\u4e8b\u4ef6\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random<\/code>\u6a21\u5757\u7684<code>normal()<\/code>\u51fd\u6570\u751f\u6210\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u3002\u53ef\u4ee5\u6307\u5b9a\u5e73\u5747\u503c\uff08mu\uff09\u548c\u6807\u51c6\u5dee\uff08sigma\uff09\uff0c\u4ee5\u63a7\u5236\u751f\u6210\u7684\u968f\u673a\u6570\u5206\u5e03\u7684\u5747\u503c\u548c\u53d8\u5316\u7a0b\u5ea6\u3002<\/p>\n<p><strong>Q2. \u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u7b26\u5408\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\uff1f<\/strong><\/p>\n<p>\u5747\u5300\u5206\u5e03\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6982\u7387\u5206\u5e03\uff0c\u5176\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u5728\u6307\u5b9a\u7684\u8303\u56f4\u5185\u662f\u5e38\u6570\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u7684<code>uniform()<\/code>\u51fd\u6570\u751f\u6210\u7b26\u5408\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u3002\u53ef\u4ee5\u6307\u5b9a\u751f\u6210\u968f\u673a\u6570\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\uff0c\u4ece\u800c\u63a7\u5236\u751f\u6210\u7684\u968f\u673a\u6570\u7684\u8303\u56f4\u3002<\/p>\n<p><strong>Q3. \u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u7b26\u5408\u6307\u6570\u5206\u5e03\u7684\u968f\u673a\u6570\uff1f<\/strong><\/p>\n<p>\u6307\u6570\u5206\u5e03\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6982\u7387\u5206\u5e03\uff0c\u5e38\u7528\u4e8e\u6a21\u62df\u4e00\u4e9b\u968f\u673a\u4e8b\u4ef6\u7684\u95f4\u9694\u65f6\u95f4\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random<\/code>\u6a21\u5757\u7684<code>exponential()<\/code>\u51fd\u6570\u751f\u6210\u7b26\u5408\u6307\u6570\u5206\u5e03\u7684\u968f\u673a\u6570\u3002\u53ef\u4ee5\u6307\u5b9a\u6307\u6570\u5206\u5e03\u7684\u6bd4\u7387\u53c2\u6570\uff08rate\uff09\uff0c\u4ece\u800c\u63a7\u5236\u751f\u6210\u7684\u968f\u673a\u6570\u7684\u5747\u503c\u548c\u53d8\u5316\u7a0b\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6309\u7167\u7279\u5b9a\u5206\u5e03\u751f\u6210\u968f\u673a\u6570\u4e3b\u8981\u4f9d\u9760\u6807\u51c6\u5e93random\u548c\u7b2c\u4e09\u65b9\u5e93\u5982NumPy\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528r [&hellip;]","protected":false},"author":3,"featured_media":176575,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176569"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=176569"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176569\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176575"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}