{"id":958944,"date":"2024-12-27T03:36:24","date_gmt":"2024-12-26T19:36:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/958944.html"},"modified":"2024-12-27T03:36:25","modified_gmt":"2024-12-26T19:36:25","slug":"python%e5%a6%82%e4%bd%95%e7%9a%84%e9%9a%8f%e6%9c%ba%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/958944.html","title":{"rendered":"python\u5982\u4f55\u7684\u968f\u673a\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101903\/9cf5316c-b87f-45ef-86ab-12362450949b.webp\" alt=\"python\u5982\u4f55\u7684\u968f\u673a\u6574\u6570\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210\u968f\u673a\u6574\u6570\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528random\u6a21\u5757\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528secrets\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u5185\u7f6e\u7684random\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\u3002\u4f7f\u7528numpy\u5e93\u5219\u9002\u5408\u9700\u8981\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\u7684\u79d1\u5b66\u8ba1\u7b97\u573a\u666f\uff0c\u800csecrets\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u66f4\u9ad8\u5b89\u5168\u6027\u548c\u968f\u673a\u6027\u7684\u573a\u5408\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001RANDOM\u6a21\u5757\u751f\u6210\u968f\u673a\u6574\u6570<\/p>\n<\/p>\n<p><p>Python\u7684random\u6a21\u5757\u662f\u5185\u7f6e\u6a21\u5757\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u51fd\u6570\u3002random\u6a21\u5757\u4e2d\u7684randint()\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528randint()\u51fd\u6570<\/li>\n<\/ol>\n<p><p>randint()\u51fd\u6570\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5728\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u542b\u8d77\u59cb\u503c\u548c\u7ec8\u6b62\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_number = random.randint(1, 10)<\/p>\n<p>print(random_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0crandom.randint(1, 10)\u5c06\u751f\u6210\u4e00\u4e2a1\u523010\uff08\u5305\u62ec1\u548c10\uff09\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528randrange()\u51fd\u6570<\/li>\n<\/ol>\n<p><p>randrange()\u51fd\u6570\u4e0erandint()\u7c7b\u4f3c\uff0c\u4f46\u5b83\u5141\u8bb8\u6307\u5b9a\u6b65\u957f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_number = random.randrange(1, 10, 2)<\/p>\n<p>print(random_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0crandom.randrange(1, 10, 2)\u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u5947\u6570\u968f\u673a\u6574\u6570\uff081, 3, 5, 7, 9\uff09\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001NUMPY\u5e93\u751f\u6210\u968f\u673a\u6574\u6570<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u548c\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\u3002np.random.randint()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528np.random.randint()\u51fd\u6570<\/li>\n<\/ol>\n<p><p>np.random.randint()\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u6216\u591a\u4e2a\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_number = np.random.randint(1, 10)<\/p>\n<p>print(random_number)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5305\u542b5\u4e2a\u968f\u673a\u6574\u6570\u7684\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>random_numbers = np.random.randint(1, 10, size=5)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cnp.random.randint(1, 10)\u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u800cnp.random.randint(1, 10, size=5)\u751f\u6210\u4e00\u4e2a\u5305\u542b5\u4e2a1\u523010\u4e4b\u95f4\u968f\u673a\u6574\u6570\u7684\u4e00\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001SECRETS\u6a21\u5757\u751f\u6210\u968f\u673a\u6574\u6570<\/p>\n<\/p>\n<p><p>secrets\u6a21\u5757\u7528\u4e8e\u751f\u6210\u66f4\u5b89\u5168\u7684\u968f\u673a\u6570\uff0c\u9002\u5408\u7528\u4e8e\u5bc6\u7801\u5b66\u76f8\u5173\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528secrets.randbelow()\u51fd\u6570<\/li>\n<\/ol>\n<p><p>secrets.randbelow()\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u5c0f\u4e8e\u6307\u5b9a\u503c\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>random_number = secrets.randbelow(10)<\/p>\n<p>print(random_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0csecrets.randbelow(10)\u751f\u6210\u4e00\u4e2a\u5c0f\u4e8e10\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528secrets.choice()\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u867d\u7136secrets.choice()\u901a\u5e38\u7528\u4e8e\u968f\u673a\u9009\u62e9\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u751f\u6210\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>random_number = secrets.choice(range(1, 10))<\/p>\n<p>print(random_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0csecrets.choice(range(1, 10))\u4ece1\u52309\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5e94\u7528\u573a\u666f\u53ca\u4f18\u5316<\/p>\n<\/p>\n<ol>\n<li>\u65e5\u5e38\u5e94\u7528<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u5927\u591a\u6570\u65e5\u5e38\u5e94\u7528\u573a\u666f\uff0c\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6574\u6570\u662f\u8db3\u591f\u7684\u3002random\u6a21\u5757\u63d0\u4f9b\u7684\u51fd\u6570\u7b80\u5355\u6613\u7528\uff0c\u5e76\u4e14\u901f\u5ea6\u76f8\u5bf9\u8f83\u5feb\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u79d1\u5b66\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>\u5728\u9700\u8981\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\u7684\u79d1\u5b66\u8ba1\u7b97\u573a\u666f\u4e0b\uff0c\u4f7f\u7528numpy\u5e93\u4f1a\u66f4\u9ad8\u6548\u3002NumPy\u7684\u968f\u673a\u6570\u751f\u6210\u51fd\u6570\u5728\u6027\u80fd\u4e0a\u4f18\u4e8erandom\u6a21\u5757\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u751f\u6210\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u9ad8\u5b89\u5168\u9700\u6c42<\/li>\n<\/ol>\n<p><p>\u5728\u6d89\u53ca\u5230\u5bc6\u7801\u5b66\u548c\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u5408\uff0csecrets\u6a21\u5757\u662f\u9996\u9009\u3002\u5b83\u63d0\u4f9b\u7684\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u548c\u4e0d\u53ef\u9884\u6d4b\u6027\u3002<\/p>\n<\/p>\n<ol start=\"4\">\n<li>\u6027\u80fd\u4f18\u5316<\/li>\n<\/ol>\n<p><p>\u5728\u751f\u6210\u5927\u91cf\u968f\u673a\u6574\u6570\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u53ef\u4ee5\u63d0\u5347\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528NumPy\u53ef\u4ee5\u5145\u5206\u5229\u7528\u5176\u77e2\u91cf\u5316\u64cd\u4f5c\u7684\u4f18\u52bf\uff0c\u5927\u5e45\u5ea6\u63d0\u9ad8\u968f\u673a\u6570\u751f\u6210\u7684\u901f\u5ea6\u3002\u540c\u65f6\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u968f\u673a\u6570\u751f\u6210\u64cd\u4f5c\u4e5f\u53ef\u4ee5\u51cf\u5c11\u6027\u80fd\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6574\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>random\u6a21\u5757\u9002\u5408\u4e00\u822c\u5e94\u7528\uff0cnumpy\u5e93\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\uff0csecrets\u6a21\u5757\u9002\u5408\u9ad8\u5b89\u5168\u6027\u9700\u6c42\u3002<\/strong>\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u5b89\u5168\u6027\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u4e86\u89e3\u5b83\u4eec\u7684\u9002\u7528\u573a\u666f\uff0c\u5c06\u6709\u52a9\u4e8e\u5728\u4e0d\u540c\u7684\u7f16\u7a0b\u4efb\u52a1\u4e2d\u7075\u6d3b\u5e94\u7528\u968f\u673a\u6570\u751f\u6210\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u6765\u751f\u6210\u968f\u673a\u6574\u6570\u3002\u5177\u4f53\u6765\u8bf4\uff0c<code>random.randint(a, b)<\/code>\u51fd\u6570\u53ef\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5305\u542b\u5728a\u548cb\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002\u8fd9\u91cc\u7684a\u548cb\u90fd\u662f\u6574\u6570\uff0c\u5e76\u4e14\u751f\u6210\u7684\u968f\u673a\u6570\u5c06\u5305\u62ec\u8fd9\u4e24\u4e2a\u8fb9\u754c\u3002\u4f8b\u5982\uff0c<code>random.randint(1, 10)<\/code>\u5c06\u8fd4\u56de1\u523010\u4e4b\u95f4\u7684\u4e00\u4e2a\u968f\u673a\u6574\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u751f\u6210\u591a\u4e2a\u968f\u673a\u6574\u6570\u800c\u4e0d\u91cd\u590d\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u751f\u6210\u591a\u4e2a\u968f\u673a\u6574\u6570\u4e14\u4e0d\u91cd\u590d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.sample()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u5141\u8bb8\u60a8\u4ece\u6307\u5b9a\u7684\u8303\u56f4\u4e2d\u968f\u673a\u9009\u62e9\u4e0d\u91cd\u590d\u7684\u6574\u6570\u3002\u4f8b\u5982\uff0c<code>random.sample(range(1, 101), 10)<\/code>\u5c06\u4ece1\u5230100\u4e4b\u95f4\u968f\u673a\u9009\u62e910\u4e2a\u4e0d\u91cd\u590d\u7684\u6574\u6570\u3002<\/p>\n<p><strong>\u4f7f\u7528\u968f\u673a\u79cd\u5b50\u6709\u4ec0\u4e48\u597d\u5904\uff1f<\/strong><br \/>\u8bbe\u7f6e\u968f\u673a\u79cd\u5b50\u662f\u4e3a\u4e86\u786e\u4fdd\u6bcf\u6b21\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u662f\u53ef\u91cd\u73b0\u7684\u3002\u901a\u8fc7\u4f7f\u7528<code>random.seed(a)<\/code>\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u6574\u6570\u4f5c\u4e3a\u79cd\u5b50\uff0c\u8fd9\u6837\u5728\u8fd0\u884c\u76f8\u540c\u7684\u4ee3\u7801\u65f6\u5c06\u59cb\u7ec8\u751f\u6210\u76f8\u540c\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002\u8fd9\u5728\u8c03\u8bd5\u6216\u9700\u8981\u91cd\u590d\u5b9e\u9a8c\u7684\u60c5\u51b5\u4e0b\u7279\u522b\u6709\u7528\u3002\u4f8b\u5982\uff0c\u901a\u8fc7<code>random.seed(42)<\/code>\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\u90fd\u5c06\u4ea7\u751f\u76f8\u540c\u7684\u968f\u673a\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210\u968f\u673a\u6574\u6570\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528random\u6a21\u5757\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528secrets\u6a21\u5757\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528 [&hellip;]","protected":false},"author":3,"featured_media":958947,"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\/958944"}],"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=958944"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958944\/revisions"}],"predecessor-version":[{"id":958949,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958944\/revisions\/958949"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/958947"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=958944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=958944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=958944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}