{"id":1125885,"date":"2025-01-08T19:55:49","date_gmt":"2025-01-08T11:55:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1125885.html"},"modified":"2025-01-08T19:55:51","modified_gmt":"2025-01-08T11:55:51","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8f%96%e4%b8%80%e4%b8%aa%e9%9a%8f%e6%9c%ba%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1125885.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u53d6\u4e00\u4e2a\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\/25090419\/4bc83c0e-2bed-4360-9240-23daf5a29f17.webp\" alt=\"python\u4e2d\u5982\u4f55\u53d6\u4e00\u4e2a\u968f\u673a\u6570\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53d6\u4e00\u4e2a\u968f\u673a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u3001\u4f7f\u7528numpy\u6a21\u5757\u3001\u901a\u8fc7\u7cfb\u7edf\u65f6\u95f4\u79cd\u5b50\u751f\u6210\u968f\u673a\u6570\u7b49<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u70b9\u548c\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u91cd\u70b9\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684random\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u662f\u751f\u6210\u968f\u673a\u6570\u7684\u6700\u5e38\u7528\u624b\u6bb5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6574\u6570\uff0c\u8303\u56f4\u57281\u523010\u4e4b\u95f4<\/strong><\/h2>\n<p>random_int = random.randint(1, 10)<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>random.randint(a, b)<\/strong> \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u4ecb\u4e8ea\u548cb\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u542ba\u548cb\u4e24\u4e2a\u7aef\u70b9\u3002\u8fd9\u5728\u9700\u8981\u751f\u6210\u7279\u5b9a\u8303\u56f4\u5185\u7684\u6574\u6570\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u8303\u56f4\u57280\u52301\u4e4b\u95f4<\/strong><\/h2>\n<p>random_float = random.random()<\/p>\n<p>print(random_float)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float_range = random.uniform(1.5, 3.5)<\/p>\n<p>print(random_float_range)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>random.random()<\/strong> \u8fd4\u56de\u4e00\u4e2a\u8303\u56f4\u57280\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u800c <strong>random.uniform(a, b)<\/strong> \u8fd4\u56de\u8303\u56f4\u5728a\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u751f\u6210\u968f\u673a\u5e8f\u5217<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>random_choice = random.choice([&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;])<\/p>\n<p>print(random_choice)<\/p>\n<h2><strong>\u6253\u4e71\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>random.shuffle(my_list)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>random.choice(seq)<\/strong> \u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\uff0c<strong>random.shuffle(seq)<\/strong> \u5219\u5c06\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u968f\u673a\u6253\u4e71\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528numpy\u6a21\u5757\u751f\u6210\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>numpy\u6a21\u5757\u540c\u6837\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\uff0c\u7279\u522b\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u968f\u673a\u6574\u6570<\/h4>\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\uff0c\u8303\u56f4\u57281\u523010\u4e4b\u95f4<\/strong><\/h2>\n<p>random_int = np.random.randint(1, 11)<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>np.random.randint(low, high)<\/strong> \u751f\u6210\u4e00\u4e2a\u4ecb\u4e8elow\u548chigh\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u542blow\u4f46\u4e0d\u5305\u542bhigh\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u57280\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float = np.random.rand()<\/p>\n<p>print(random_float)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float_range = np.random.uniform(1.5, 3.5)<\/p>\n<p>print(random_float_range)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>np.random.rand()<\/strong> \u8fd4\u56de\u4e00\u4e2a\u8303\u56f4\u57280\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u800c <strong>np.random.uniform(low, high)<\/strong> \u8fd4\u56de\u8303\u56f4\u5728low\u5230high\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u751f\u6210\u968f\u673a\u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a2x3\u7684\u968f\u673a\u6570\u7ec4\uff0c\u503c\u57280\u52301\u4e4b\u95f4<\/strong><\/h2>\n<p>random_array = np.random.rand(2, 3)<\/p>\n<p>print(random_array)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a2x3\u7684\u968f\u673a\u6574\u6570\u6570\u7ec4\uff0c\u503c\u57281\u523010\u4e4b\u95f4<\/strong><\/h2>\n<p>random_int_array = np.random.randint(1, 11, size=(2, 3))<\/p>\n<p>print(random_int_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u79cd\u5b50\u751f\u6210\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4e3a\u4e86\u751f\u6210\u53ef\u91cd\u590d\u7684\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u4f5c\u4e3a\u79cd\u5b50\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import time<\/p>\n<h2><strong>\u4f7f\u7528\u5f53\u524d\u65f6\u95f4\u4f5c\u4e3a\u79cd\u5b50<\/strong><\/h2>\n<p>random.seed(time.time())<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_int = random.randint(1, 10)<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>random.seed(a)<\/strong> \u51fd\u6570\u7528\u6765\u521d\u59cb\u5316\u968f\u673a\u6570\u751f\u6210\u5668\u7684\u79cd\u5b50\u3002\u901a\u8fc7\u8bbe\u7f6e\u76f8\u540c\u7684\u79cd\u5b50\uff0c\u53ef\u4ee5\u751f\u6210\u76f8\u540c\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u6e38\u620f\u5f00\u53d1<\/h4>\n<\/p>\n<p><p>\u968f\u673a\u6570\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\uff0c\u5982\u751f\u6210\u968f\u673a\u5730\u56fe\u3001\u968f\u673a\u6389\u843d\u7269\u54c1\u7b49\u3002random\u6a21\u5757\u7684\u7b80\u5355\u6613\u7528\u4f7f\u5176\u6210\u4e3a\u6e38\u620f\u5f00\u53d1\u4e2d\u7684\u9996\u9009\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0cnumpy\u6a21\u5757\u7684\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\u66f4\u4e3a\u5f3a\u5927\u548c\u7075\u6d3b\u3002\u5b83\u80fd\u751f\u6210\u591a\u7ef4\u6570\u7ec4\uff0c\u4e14\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5b89\u5168\u6027<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u5e94\u7528\uff0c\u5982\u5bc6\u7801\u751f\u6210\uff0c\u5efa\u8bae\u4f7f\u7528secrets\u6a21\u5757\u800c\u4e0d\u662frandom\u6a21\u5757\uff0c\u56e0\u4e3asecrets\u6a21\u5757\u751f\u6210\u7684\u968f\u673a\u6570\u66f4\u96be\u4ee5\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5b89\u5168\u7684\u968f\u673a\u6574\u6570\uff0c\u8303\u56f4\u57281\u523010\u4e4b\u95f4<\/strong><\/h2>\n<p>secure_random_int = secrets.randbelow(10) + 1<\/p>\n<p>print(secure_random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u4e3b\u8981\u5305\u62ecrandom\u6a21\u5757\u3001numpy\u6a21\u5757\u548c\u901a\u8fc7\u7cfb\u7edf\u65f6\u95f4\u79cd\u5b50\u751f\u6210\u968f\u673a\u6570\u7b49\u3002<strong>random\u6a21\u5757\u529f\u80fd\u4e30\u5bcc\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u65e5\u5e38\u5e94\u7528\uff0cnumpy\u6a21\u5757\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\uff0c\u800csecrets\u6a21\u5757\u7528\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u573a\u666f<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\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\u4e00\u4e2a\u968f\u673a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u751f\u6210\u968f\u673a\u6570\u901a\u5e38\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u3002\u53ef\u4ee5\u4f7f\u7528<code>random.randint(a, b)<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5728<code>a<\/code>\u5230<code>b<\/code>\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u542b<code>a<\/code>\u548c<code>b<\/code>\u3002\u4f8b\u5982\uff0c<code>random.randint(1, 10)<\/code>\u4f1a\u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<p><strong>\u751f\u6210\u6d6e\u70b9\u968f\u673a\u6570\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u751f\u6210\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8303\u56f4\u5728<code>a<\/code>\u548c<code>b<\/code>\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u4f8b\u5982\uff0c<code>random.uniform(1.5, 5.5)<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u57281.5\u52305.5\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e\u968f\u673a\u6570\u7684\u79cd\u5b50\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\u751f\u6210\u76f8\u540c\u7684\u968f\u673a\u6570\u5e8f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.seed(value)<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u79cd\u5b50\u503c\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u6216\u9700\u8981\u91cd\u73b0\u7ed3\u679c\u7684\u573a\u666f\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u8c03\u7528<code>random.seed(42)<\/code>\u540e\uff0c\u540e\u7eed\u751f\u6210\u7684\u968f\u673a\u6570\u5c06\u662f\u53ef\u9884\u6d4b\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53d6\u4e00\u4e2a\u968f\u673a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u3001\u4f7f\u7528numpy\u6a21\u5757\u3001\u901a\u8fc7\u7cfb\u7edf\u65f6\u95f4\u79cd\u5b50\u751f [&hellip;]","protected":false},"author":3,"featured_media":1125890,"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\/1125885"}],"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=1125885"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125885\/revisions"}],"predecessor-version":[{"id":1125893,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125885\/revisions\/1125893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1125890"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1125885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1125885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1125885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}