{"id":1072772,"date":"2025-01-08T11:19:13","date_gmt":"2025-01-08T03:19:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072772.html"},"modified":"2025-01-08T11:19:16","modified_gmt":"2025-01-08T03:19:16","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e9%80%89%e6%8b%a9%e4%b8%89%e4%bd%8d%e6%95%b4%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1072772.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102903\/4cfcd9d8-63f9-4b8a-8dc4-f935daf46eb8.webp\" alt=\"python\u5982\u4f55\u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570\" \/><\/p>\n<p><p> <strong>Python \u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570\u7684\u51e0\u79cd\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570\uff0c\u4f8b\u5982\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u3001<code>numpy<\/code>\u5e93\u7b49\u3002<strong>\u5e38\u89c1\u7684\u51e0\u79cd\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528random.randint()\u3001random.sample()\u3001random.choices()\u3001numpy.random.randint()<\/strong>\u3002\u5176\u4e2d\uff0c<code>random.randint()<\/code>\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\u4e14\u4e0d\u9700\u8981\u989d\u5916\u7684\u4f9d\u8d56\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528random.randint()\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>random.randint(a, b)<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684<code>random<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u4f4d\u4e8ea\u548cb\uff08\u5305\u62eca\u548cb\uff09\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002\u8981\u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570\uff0c\u53ef\u4ee5\u5c06a\u8bbe\u7f6e\u4e3a100\uff0cb\u8bbe\u7f6e\u4e3a999\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_int = random.randint(100, 999)<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528<code>random.randint(100, 999)<\/code>\u751f\u6210\u4e00\u4e2a100\u5230999\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u5e76\u6253\u5370\u51fa\u6765\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u66f4\u8be6\u7ec6\u5730\u8bb2\u89e3\u548c\u5c55\u793a\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528random\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001random.randint()<\/h4>\n<\/p>\n<p><p><code>random.randint()<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u901a\u8fc7\u6307\u5b9a\u8303\u56f4100\u5230999\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u751f\u6210\u7684\u6574\u6570\u662f\u4e09\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u4e09\u4f4d\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_int = random.randint(100, 999)<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u548c\u7b80\u5355\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001random.sample()<\/h4>\n<\/p>\n<p><p><code>random.sample()<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u6307\u5b9a\u7684\u5e8f\u5217\u4e2d\u968f\u673a\u62bd\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\u3002\u8981\u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570\uff0c\u53ef\u4ee5\u5148\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u4e09\u4f4d\u6574\u6570\u7684\u5217\u8868\uff0c\u7136\u540e\u4ece\u4e2d\u62bd\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u6240\u6709\u4e09\u4f4d\u6570\u7684\u5217\u8868<\/strong><\/h2>\n<p>three_digit_numbers = list(range(100, 1000))<\/p>\n<h2><strong>\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u4e09\u4f4d\u6574\u6570<\/strong><\/h2>\n<p>random_int = random.sample(three_digit_numbers, 1)[0]<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u9002\u5408\u9700\u8981\u4ece\u4e00\u4e2a\u7279\u5b9a\u8303\u56f4\u5185\u8fdb\u884c\u91c7\u6837\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>3\u3001random.choices()<\/h4>\n<\/p>\n<p><p><code>random.choices()<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u6307\u5b9a\u7684\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\uff0c\u5e76\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u6240\u6709\u4e09\u4f4d\u6570\u7684\u5217\u8868<\/strong><\/h2>\n<p>three_digit_numbers = list(range(100, 1000))<\/p>\n<h2><strong>\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u4e09\u4f4d\u6574\u6570<\/strong><\/h2>\n<p>random_int = random.choices(three_digit_numbers, k=1)[0]<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e<code>random.sample()<\/code>\u4e0d\u540c\uff0c<code>random.choices()<\/code>\u5141\u8bb8\u91cd\u590d\u9009\u62e9\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528\u4e8e\u9700\u8981\u591a\u6b21\u9009\u62e9\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><h4>1\u3001numpy.random.randint()<\/h4>\n<\/p>\n<p><p><code>numpy.random.randint()<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u4e0e<code>random.randint()<\/code>\u7c7b\u4f3c\uff0c\u4f46\u5b83\u652f\u6301\u751f\u6210\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u4e09\u4f4d\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_int = np.random.randint(100, 1000)<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u751f\u6210\u5927\u91cf\u968f\u673a\u6574\u6570\u6216\u591a\u7ef4\u6570\u7ec4\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4e0d\u5e38\u89c1\uff0c\u4f46\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c<code>random.choice()<\/code>\u51fd\u6570\u7ec4\u5408\u6765\u5b9e\u73b0\u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u6240\u6709\u4e09\u4f4d\u6570\u7684\u5217\u8868<\/strong><\/h2>\n<p>three_digit_numbers = [i for i in range(100, 1000)]<\/p>\n<h2><strong>\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u4e09\u4f4d\u6574\u6570<\/strong><\/h2>\n<p>random_int = random.choice(three_digit_numbers)<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5c01\u88c5\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u4e8e\u5728\u9879\u76ee\u4e2d\u591a\u6b21\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def generate_random_three_digit():<\/p>\n<p>    return random.randint(100, 999)<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u4e09\u4f4d\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_int = generate_random_three_digit()<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u5176\u4ed6\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u6a21\u5757\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u7ed3\u5408<code>time<\/code>\u6a21\u5757\u751f\u6210\u57fa\u4e8e\u65f6\u95f4\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import time<\/p>\n<p>def generate_time_based_random():<\/p>\n<p>    random.seed(time.time())<\/p>\n<p>    return random.randint(100, 999)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u57fa\u4e8e\u65f6\u95f4\u7684\u4e09\u4f4d\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_int = generate_time_based_random()<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u9700\u8981\u57fa\u4e8e\u65f6\u95f4\u53d8\u5316\u751f\u6210\u968f\u673a\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u5e94\u7528\u573a\u666f\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528random\u6a21\u5757\u4e2d\u7684random.randint()\u3001random.sample()\u3001random.choices()\u4ee5\u53canumpy\u5e93\u4e2d\u7684numpy.random.randint()<\/strong>\u3002\u901a\u8fc7\u7ed3\u5408\u4e0d\u540c\u7684\u65b9\u6cd5\u548c\u6a21\u5757\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5404\u79cd\u590d\u6742\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u968f\u673a\u6570\u751f\u6210\u5728\u5f88\u591a\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4f8b\u5982\u6e38\u620f\u5f00\u53d1\u3001\u6570\u636e\u6a21\u62df\u3001\u5bc6\u7801\u5b66\u7b49\u3002\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u4f7f\u7528\u968f\u673a\u6570\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u751f\u6210\u4e09\u4f4d\u968f\u673a\u6574\u6570\u7684\u65b9\u6cd5\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\u7684\u4e09\u4f4d\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u6765\u751f\u6210\u968f\u673a\u6570\u3002\u8981\u751f\u6210\u4e09\u4f4d\u6570\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.randint(100, 999)<\/code>\uff0c\u8be5\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a100\u5230999\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002\u786e\u4fdd\u5728\u8c03\u7528\u4e4b\u524d\u5bfc\u5165<code>random<\/code>\u6a21\u5757\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u901a\u8fc7\u54ea\u4e9b\u65b9\u6cd5\u6765\u786e\u4fdd\u751f\u6210\u7684\u4e09\u4f4d\u6574\u6570\u4e0d\u91cd\u590d\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u751f\u6210\u7684\u4e09\u4f4d\u6574\u6570\u4e0d\u91cd\u590d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6216\u5217\u8868\u6765\u5b58\u50a8\u751f\u6210\u7684\u6570\u5b57\uff0c\u5e76\u5728\u6bcf\u6b21\u751f\u6210\u65b0\u6570\u5b57\u65f6\u68c0\u67e5\u8be5\u6570\u5b57\u662f\u5426\u5df2\u5b58\u5728\u3002\u5982\u679c\u5b58\u5728\uff0c\u5219\u7ee7\u7eed\u751f\u6210\u65b0\u6570\u5b57\uff0c\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u672a\u88ab\u4f7f\u7528\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u591a\u4e2a\u968f\u673a\u4e09\u4f4d\u6574\u6570\uff1f<\/strong><br \/>\u5982\u679c\u60f3\u8981\u751f\u6210\u591a\u4e2a\u968f\u673a\u4e09\u4f4d\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216\u8005\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>random.randint(100, 999)<\/code>\u6765\u751f\u6210\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u968f\u673a\u4e09\u4f4d\u6574\u6570\u7684\u5217\u8868\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5e0c\u671b\u751f\u6210\u7684\u6570\u4e0d\u91cd\u590d\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6765\u8ddf\u8e2a\u5df2\u751f\u6210\u7684\u6570\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570\u7684\u51e0\u79cd\u65b9\u6cd5 \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u968f\u673a\u9009\u62e9\u4e09\u4f4d\u6574\u6570\uff0c\u4f8b\u5982\u4f7f\u7528rand [&hellip;]","protected":false},"author":3,"featured_media":1072780,"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\/1072772"}],"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=1072772"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072772\/revisions"}],"predecessor-version":[{"id":1072781,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072772\/revisions\/1072781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1072780"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1072772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1072772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1072772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}