{"id":1124532,"date":"2025-01-08T19:42:34","date_gmt":"2025-01-08T11:42:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1124532.html"},"modified":"2025-01-08T19:42:37","modified_gmt":"2025-01-08T11:42:37","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e6%95%b4%e6%95%b0%e8%8c%83%e5%9b%b4%e5%86%85%e9%9a%8f%e6%9c%ba%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1124532.html","title":{"rendered":"python\u5982\u4f55\u5728\u6574\u6570\u8303\u56f4\u5185\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\/25085547\/66a817c0-9dc7-4522-8906-2974aa63e7fd.webp\" alt=\"python\u5982\u4f55\u5728\u6574\u6570\u8303\u56f4\u5185\u968f\u673a\u6570\" \/><\/p>\n<p><p> <strong>Python\u5728\u6574\u6570\u8303\u56f4\u5185\u751f\u6210\u968f\u673a\u6570\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528random\u6a21\u5757\u3001\u5229\u7528numpy\u5e93\u3001\u4f9d\u8d56secrets\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u4e14\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u6807\u51c6\u5e93\u4e2d\u7684random\u6a21\u5757\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6574\u6570\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684random\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u3002\u6700\u5e38\u89c1\u7684\u4e24\u79cd\u65b9\u6cd5\u662f<code>randint()<\/code>\u548c<code>randrange()<\/code>\u3002<code>randint()<\/code>\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5305\u542b\u5728\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u800c<code>randrange()<\/code>\u5219\u66f4\u4e3a\u7075\u6d3b\uff0c\u53ef\u4ee5\u6307\u5b9a\u6b65\u957f\u3002\u4ee5\u4e0b\u662f\u8fd9\u4e24\u79cd\u65b9\u6cd5\u7684\u8be6\u7ec6\u4ecb\u7ecd\u548c\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001random\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001randint()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>randint()<\/code>\u65b9\u6cd5\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5728\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u542b\u8303\u56f4\u7684\u4e24\u4e2a\u7aef\u70b9\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random.randint(a, b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>a<\/code>\u548c<code>b<\/code>\u662f\u6574\u6570\uff0c<code>randint()<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u5728<code>a<\/code>\u5230<code>b<\/code>\uff08\u5305\u542b<code>a<\/code>\u548c<code>b<\/code>\uff09\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/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(&quot;Random number between 1 and 10:&quot;, random_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>random.randint(1, 10)<\/code>\u5c06\u751f\u6210\u4e00\u4e2a\u57281\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u62ec1\u548c10\u3002<\/p>\n<\/p>\n<p><h4>2\u3001randrange()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>randrange()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6307\u5b9a\u8303\u56f4\u548c\u6b65\u957f\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random.randrange(start, stop[, step])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>start<\/code>\u662f\u8303\u56f4\u7684\u8d77\u70b9\uff08\u5305\u542b\uff09\uff0c<code>stop<\/code>\u662f\u8303\u56f4\u7684\u7ec8\u70b9\uff08\u4e0d\u5305\u542b\uff09\uff0c<code>step<\/code>\u662f\u6b65\u957f\uff08\u53ef\u9009\uff09\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/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(&quot;Random number between 1 and 10 with step 2:&quot;, random_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>random.randrange(1, 10, 2)<\/code>\u5c06\u751f\u6210\u4e00\u4e2a\u57281\u52309\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u6b65\u957f\u4e3a2\uff0c\u56e0\u6b64\u53ef\u80fd\u7684\u7ed3\u679c\u662f1, 3, 5, 7, 9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001numpy\u5e93<\/h3>\n<\/p>\n<p><p>numpy\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5176\u4e2d\u4e5f\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u529f\u80fd\u3002\u4f7f\u7528numpy\u751f\u6210\u968f\u673a\u6574\u6570\u7684\u65b9\u6cd5\u662f<code>numpy.random.randint()<\/code>\u3002<\/p>\n<\/p>\n<p><h4>1\u3001numpy.random.randint()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>numpy.random.randint()<\/code>\u65b9\u6cd5\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u6216\u591a\u4e2a\u5728\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>np.random.randint(low, high=None, size=None, dtype=&#39;l&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>low<\/code>\u662f\u8303\u56f4\u7684\u4e0b\u754c\uff08\u5305\u542b\uff09\uff0c<code>high<\/code>\u662f\u8303\u56f4\u7684\u4e0a\u754c\uff08\u4e0d\u5305\u542b\uff09\uff0c<code>size<\/code>\u662f\u751f\u6210\u968f\u673a\u6570\u7684\u6570\u91cf\uff08\u53ef\u9009\uff09\uff0c<code>dtype<\/code>\u662f\u6570\u636e\u7c7b\u578b\uff08\u53ef\u9009\uff09\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>random_number = np.random.randint(1, 10)<\/p>\n<p>print(&quot;Random number between 1 and 10:&quot;, random_number)<\/p>\n<p>random_numbers = np.random.randint(1, 10, size=5)<\/p>\n<p>print(&quot;Array of 5 random numbers between 1 and 10:&quot;, random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>np.random.randint(1, 10)<\/code>\u5c06\u751f\u6210\u4e00\u4e2a\u57281\u52309\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u800c<code>np.random.randint(1, 10, size=5)<\/code>\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b5\u4e2a\u57281\u52309\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001secrets\u6a21\u5757<\/h3>\n<\/p>\n<p><p>secrets\u6a21\u5757\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u4e2a\u7528\u4e8e\u751f\u6210\u52a0\u5bc6\u5f3a\u968f\u673a\u6570\u7684\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u52a0\u5b89\u5168\u7684\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001secrets.randbelow()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>secrets.randbelow()<\/code>\u65b9\u6cd5\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u57280\u5230\u6307\u5b9a\u4e0a\u9650\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff08\u4e0d\u5305\u542b\u4e0a\u9650\uff09\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>secrets.randbelow(upper)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>upper<\/code>\u662f\u4e0a\u9650\uff08\u4e0d\u5305\u542b\uff09\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>random_number = secrets.randbelow(10)<\/p>\n<p>print(&quot;Random number between 0 and 9:&quot;, random_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>secrets.randbelow(10)<\/code>\u5c06\u751f\u6210\u4e00\u4e2a\u57280\u52309\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001secrets.choice()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>secrets.choice()<\/code>\u65b9\u6cd5\u7528\u4e8e\u4ece\u6307\u5b9a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>secrets.choice(sequence)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>sequence<\/code>\u662f\u4e00\u4e2a\u975e\u7a7a\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/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(&quot;Random number between 1 and 9:&quot;, random_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>secrets.choice(range(1, 10))<\/code>\u5c06\u4ece1\u52309\u7684\u8303\u56f4\u5185\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6574\u6570\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6700\u5e38\u7528\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528random\u6a21\u5757\u7684<code>randint()<\/code>\u548c<code>randrange()<\/code>\u65b9\u6cd5\u3002\u5bf9\u4e8e\u9700\u8981\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\u7684\u573a\u666f\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u800c\u5bf9\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u573a\u666f\uff0csecrets\u6a21\u5757\u662f\u6700\u4f73\u9009\u62e9\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u505a\u51fa\u5408\u7406\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4f53\u6765\u8bf4\uff0c<strong>\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6574\u6570\u6700\u4e3a\u76f4\u63a5\u548c\u5e38\u7528\uff0cnumpy\u5e93\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u573a\u666f\uff0csecrets\u6a21\u5757\u9002\u7528\u4e8e\u9ad8\u5b89\u5168\u6027\u573a\u666f\u3002<\/strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u751f\u6210\u4e00\u4e2a\u7279\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u751f\u6210\u7279\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>random.randint(a, b)<\/code>\u51fd\u6570\uff0c\u5176\u4e2d<code>a<\/code>\u662f\u8303\u56f4\u7684\u4e0b\u9650\uff0c<code>b<\/code>\u662f\u8303\u56f4\u7684\u4e0a\u9650\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u62ec<code>a<\/code>\u548c<code>b<\/code>\u5728\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u4f8b\u5982\uff0c<code>random.randint(1, 10)<\/code>\u5c06\u8fd4\u56de1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u751f\u6210\u4e0d\u5305\u62ec\u67d0\u4e9b\u7279\u5b9a\u6574\u6570\u7684\u968f\u673a\u6570\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5e0c\u671b\u751f\u6210\u7684\u968f\u673a\u6574\u6570\u4e0d\u5305\u62ec\u67d0\u4e9b\u7279\u5b9a\u7684\u6570\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.choice()<\/code>\u7ed3\u5408\u751f\u6210\u4e00\u4e2a\u5217\u8868\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u6784\u5efa\u4e00\u4e2a\u5305\u542b\u4f60\u5e0c\u671b\u751f\u6210\u7684\u6240\u6709\u6574\u6570\u7684\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>random.choice()<\/code>\u4ece\u4e2d\u968f\u673a\u9009\u62e9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u57281\u523010\u4e4b\u95f4\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\uff0c\u4f46\u4e0d\u5305\u62ec5\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>random.choice([x for x in range(1, 11) if x != 5])<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bbe\u7f6e\u968f\u673a\u6570\u7684\u79cd\u5b50\u4ee5\u4fdd\u8bc1\u7ed3\u679c\u53ef\u91cd\u590d\uff1f<\/strong><br \/>\u82e5\u5e0c\u671b\u968f\u673a\u6570\u7684\u751f\u6210\u7ed3\u679c\u53ef\u91cd\u590d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.seed()<\/code>\u51fd\u6570\u3002\u901a\u8fc7\u8bbe\u7f6e\u76f8\u540c\u7684\u79cd\u5b50\u503c\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u90fd\u4f1a\u4e00\u81f4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>random.seed(42)<\/code>\uff0c\u7136\u540e\u8c03\u7528<code>random.randint(1, 10)<\/code>\uff0c\u5728\u591a\u6b21\u8fd0\u884c\u4e2d\u90fd\u4f1a\u5f97\u5230\u76f8\u540c\u7684\u968f\u673a\u6570\u3002\u8fd9\u5728\u8c03\u8bd5\u548c\u6d4b\u8bd5\u4e2d\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u786e\u4fdd\u7ed3\u679c\u7684\u4e00\u81f4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5728\u6574\u6570\u8303\u56f4\u5185\u751f\u6210\u968f\u673a\u6570\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528random\u6a21\u5757\u3001\u5229\u7528numpy\u5e93\u3001\u4f9d\u8d56secrets\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1124547,"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\/1124532"}],"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=1124532"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124532\/revisions"}],"predecessor-version":[{"id":1124549,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124532\/revisions\/1124549"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1124547"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1124532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1124532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1124532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}