{"id":1081348,"date":"2025-01-08T12:38:47","date_gmt":"2025-01-08T04:38:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1081348.html"},"modified":"2025-01-08T12:38:49","modified_gmt":"2025-01-08T04:38:49","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e8%be%93%e5%87%ba%e9%9a%8f%e6%9c%ba%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1081348.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u8f93\u51fa\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\/24183425\/49f34661-b21f-4c24-9300-cf86a6d35057.webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u8f93\u51fa\u968f\u673a\u6570\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\u8f93\u51fa\u968f\u673a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f8b\u5982\u4f7f\u7528 random \u6a21\u5757\u3001secrets \u6a21\u5757\u3001Numpy \u5e93\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 random \u6a21\u5757\uff0c\u8fd9\u662f\u56e0\u4e3a\u5b83\u5305\u542b\u4e86\u751f\u6210\u5404\u79cd\u7c7b\u578b\u968f\u673a\u6570\u7684\u591a\u4e2a\u51fd\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>random \u6a21\u5757<\/strong>\u662f Python \u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u751f\u6210\u6574\u6570\u3001\u6d6e\u70b9\u6570\u4ee5\u53ca\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u5143\u7d20\u3002\u8981\u4f7f\u7528 random \u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u5b83\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 random \u6a21\u5757\u751f\u6210\u4e0d\u540c\u7c7b\u578b\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165 random \u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528 random \u6a21\u5757\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5bfc\u5165\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u751f\u6210\u968f\u673a\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u8981\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>random.randint(a, b)<\/code> \u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5728 <code>a<\/code> \u548c <code>b<\/code> \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff08\u5305\u62ec <code>a<\/code> \u548c <code>b<\/code>\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_integer = random.randint(1, 10)<\/p>\n<p>print(random_integer)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p>\u8981\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>random.uniform(a, b)<\/code> \u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5728 <code>a<\/code> \u548c <code>b<\/code> \u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_float = random.uniform(1.0, 10.0)<\/p>\n<p>print(random_float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u4e00\u4e2a1.0\u523010.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u8981\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>random.choice(seq)<\/code> \u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_element = random.choice([&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;])<\/p>\n<p>print(random_element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u4ece\u5217\u8868 <code>[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/code> \u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u751f\u6210\u968f\u673a\u5e8f\u5217<\/h3>\n<\/p>\n<p><p>\u8981\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5e8f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>random.sample(seq, k)<\/code> \u51fd\u6570\uff0c\u5b83\u4f1a\u4ece\u5e8f\u5217 <code>seq<\/code> \u4e2d\u968f\u673a\u9009\u53d6 <code>k<\/code> \u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_sample = random.sample(range(1, 11), 3)<\/p>\n<p>print(random_sample)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u4ece1\u523010\u7684\u8303\u56f4\u5185\u968f\u673a\u9009\u53d63\u4e2a\u6574\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8fd93\u4e2a\u6574\u6570\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u751f\u6210\u968f\u673a\u79cd\u5b50<\/h3>\n<\/p>\n<p><p>\u8981\u751f\u6210\u4e00\u4e2a\u53ef\u91cd\u590d\u7684\u968f\u673a\u6570\u5e8f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>random.seed(a)<\/code> \u51fd\u6570\u6765\u8bbe\u7f6e\u968f\u673a\u6570\u751f\u6210\u5668\u7684\u79cd\u5b50\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random.seed(10)<\/p>\n<p>print(random.randint(1, 10))<\/p>\n<p>random.seed(10)<\/p>\n<p>print(random.randint(1, 10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u76f8\u540c\u7684\u968f\u673a\u6570\uff0c\u56e0\u4e3a\u79cd\u5b50\u503c\u662f\u76f8\u540c\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528 secrets \u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u66f4\u9ad8\u5b89\u5168\u6027\u7684\u968f\u673a\u6570\u751f\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528 secrets \u6a21\u5757\u3002\u8fd9\u4e2a\u6a21\u5757\u4e13\u4e3a\u751f\u6210\u52a0\u5bc6\u5b89\u5168\u7684\u968f\u673a\u6570\u800c\u8bbe\u8ba1\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>secure_random = secrets.randbelow(10)<\/p>\n<p>print(secure_random)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u4e00\u4e2a0\u52309\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528 numpy \u751f\u6210\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>Numpy \u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u5b83\u4e5f\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u591a\u79cd\u65b9\u6cd5\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5 numpy \u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528 numpy \u751f\u6210\u968f\u673a\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>random_array = np.random.rand(3, 3)<\/p>\n<p>print(random_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u4e00\u4e2a3&#215;3\u7684\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\u8f93\u51fa\u968f\u673a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u7528\u7684\u6709\u4f7f\u7528 random \u6a21\u5757\u3001secrets \u6a21\u5757\u548c numpy \u5e93\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u751f\u6210\u968f\u673a\u6570\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u7f16\u7a0b\u4e2d\u66f4\u7075\u6d3b\u5730\u4f7f\u7528\u968f\u673a\u6570\uff0c\u4ece\u800c\u63d0\u5347\u4ee3\u7801\u7684\u529f\u80fd\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u6765\u751f\u6210\u968f\u673a\u6570\u3002\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\u3002\u53ef\u4ee5\u4f7f\u7528<code>random.randint(a, b)<\/code>\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6574\u6570\uff0c\u6216\u8005\u4f7f\u7528<code>random.uniform(a, b)<\/code>\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6d6e\u70b9\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import random\n\n# \u751f\u62101\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\nrandom_integer = random.randint(1, 10)\nprint(random_integer)\n\n# \u751f\u62101\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\nrandom_float = random.uniform(1, 10)\nprint(random_float)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\uff1f<\/strong><br \/>\u7edd\u5bf9\u53ef\u4ee5\u3002\u4f7f\u7528<code>random.sample()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u4ece\u6307\u5b9a\u7684\u8303\u56f4\u4e2d\u751f\u6210\u591a\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u4ece1\u5230100\u4e2d\u968f\u673a\u9009\u62e95\u4e2a\u4e0d\u91cd\u590d\u7684\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">random_numbers = random.sample(range(1, 101), 5)\nprint(random_numbers)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u6570\u4e0d\u91cd\u590d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u968f\u673a\u6570\u7684\u79cd\u5b50\uff1f<\/strong><br \/>\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50\u53ef\u4ee5\u4fdd\u8bc1\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u76f8\u540c\u3002\u53ef\u4ee5\u4f7f\u7528<code>random.seed()<\/code>\u51fd\u6570\u8bbe\u7f6e\u79cd\u5b50\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">random.seed(42)  # \u8bbe\u7f6e\u79cd\u5b50\nprint(random.randint(1, 10))  # \u6bcf\u6b21\u8fd0\u884c\u90fd\u5c06\u8f93\u51fa\u76f8\u540c\u7684\u968f\u673a\u6570\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8bbe\u7f6e\u79cd\u5b50\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u590d\u73b0\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\u8f93\u51fa\u968f\u673a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f8b\u5982\u4f7f\u7528 random \u6a21\u5757\u3001secrets \u6a21\u5757\u3001Numpy \u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1081358,"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\/1081348"}],"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=1081348"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081348\/revisions"}],"predecessor-version":[{"id":1081362,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081348\/revisions\/1081362"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1081358"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1081348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1081348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1081348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}