{"id":1102128,"date":"2025-01-08T15:56:39","date_gmt":"2025-01-08T07:56:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1102128.html"},"modified":"2025-01-08T15:56:42","modified_gmt":"2025-01-08T07:56:42","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e8%be%93%e5%87%ba%e4%b8%80%e4%b8%aa%e6%95%b0%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1102128.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u8f93\u51fa\u4e00\u4e2a\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064556\/0d3efbb7-3956-40c6-b4ad-0fa00fa67037.webp\" alt=\"python\u5982\u4f55\u968f\u673a\u8f93\u51fa\u4e00\u4e2a\u6570\u5b57\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528random\u6a21\u5757\u6765\u968f\u673a\u8f93\u51fa\u4e00\u4e2a\u6570\u5b57\u3001random\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u751f\u6210\u968f\u673a\u6570\u3001\u53ef\u4ee5\u751f\u6210\u6574\u6570\u3001\u5c0f\u6570\u4ee5\u53ca\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\u3002random.randint()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u5728\u6307\u5b9a\u7684\u8303\u56f4\u5185\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6574\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u751f\u6210\u968f\u673a\u6570\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165random\u6a21\u5757\uff0c\u7136\u540e\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165random\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528random\u6a21\u5757\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5bfc\u5165\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\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\u6307\u5b9a\u8303\u56f4\u7684\u968f\u673a\u6574\u6570<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528random.randint()\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002<\/strong>\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\u8303\u56f4\u7684\u4e0b\u9650\u548c\u4e0a\u9650\uff08\u5305\u62ec\u4e24\u7aef\uff09\u3002\u4f8b\u5982\uff0c\u8981\u751f\u62101\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/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\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crandom.randint(1, 10)\u5c06\u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cfrandom_number\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u751f\u6210\u6307\u5b9a\u8303\u56f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528random.uniform()\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/strong>\u8be5\u51fd\u6570\u4e5f\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\u8303\u56f4\u7684\u4e0b\u9650\u548c\u4e0a\u9650\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crandom.uniform(1.0, 10.0)\u5c06\u751f\u6210\u4e00\u4e2a1.0\u523010.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cfrandom_float\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u751f\u62100\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528random.random()\u51fd\u6570\u53ef\u4ee5\u751f\u62100\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/strong>\u8be5\u51fd\u6570\u4e0d\u9700\u8981\u4efb\u4f55\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_float = random.random()<\/p>\n<p>print(random_float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crandom.random()\u5c06\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cfrandom_float\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\uff08\u4e0d\u5305\u62ec\u4e0a\u9650\uff09<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528random.randrange()\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u4f46\u4e0d\u5305\u62ec\u4e0a\u9650\u3002<\/strong>\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5230\u4e09\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\u8303\u56f4\u7684\u4e0b\u9650\u3001\u4e0a\u9650\u548c\u6b65\u957f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crandom.randrange(1, 10, 2)\u5c06\u751f\u6210\u4e00\u4e2a1\u52309\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5e76\u4e14\u6b65\u957f\u4e3a2\u3002\u751f\u6210\u7684\u968f\u673a\u6570\u5c06\u662f1\u30013\u30015\u30017\u62169\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u751f\u6210\u968f\u673a\u6570\u7684\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u57fa\u672c\u7528\u6cd5\uff0crandom\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u4f8b\u5982\u751f\u6210\u968f\u673a\u5e8f\u5217\u3001\u6253\u4e71\u5e8f\u5217\u987a\u5e8f\u7b49\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u9ad8\u7ea7\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u968f\u673a\u5e8f\u5217<\/h4>\n<\/p>\n<p><p><strong>\u4f7f\u7528random.sample()\u51fd\u6570\u53ef\u4ee5\u4ece\u6307\u5b9a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\u3002<\/strong>\u4f8b\u5982\uff0c\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e93\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>random_sample = random.sample(sequence, 3)<\/p>\n<p>print(random_sample)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crandom.sample(sequence, 3)\u5c06\u4ece\u5217\u8868sequence\u4e2d\u968f\u673a\u9009\u62e93\u4e2a\u5143\u7d20\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cfrandom_sample\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6253\u4e71\u5e8f\u5217\u987a\u5e8f<\/h4>\n<\/p>\n<p><p><strong>\u4f7f\u7528random.shuffle()\u51fd\u6570\u53ef\u4ee5\u6253\u4e71\u6307\u5b9a\u5e8f\u5217\u7684\u987a\u5e8f\u3002<\/strong>\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5217\u8868\u7684\u987a\u5e8f\u6253\u4e71\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>random.shuffle(sequence)<\/p>\n<p>print(sequence)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crandom.shuffle(sequence)\u5c06\u6253\u4e71\u5217\u8868sequence\u7684\u987a\u5e8f\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u751f\u6210\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570<\/h4>\n<\/p>\n<p><p><strong>\u4f7f\u7528random.gauss()\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<\/strong>\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\u5747\u503c\u548c\u6807\u51c6\u5dee\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>mean = 0<\/p>\n<p>std_dev = 1<\/p>\n<p>random_gaussian = random.gauss(mean, std_dev)<\/p>\n<p>print(random_gaussian)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crandom.gauss(mean, std_dev)\u5c06\u751f\u6210\u4e00\u4e2a\u7b26\u5408\u5747\u503c\u4e3a0\u3001\u6807\u51c6\u5dee\u4e3a1\u7684\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cfrandom_gaussian\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6570\u975e\u5e38\u65b9\u4fbf\u3002\u65e0\u8bba\u662f\u751f\u6210\u6574\u6570\u3001\u6d6e\u70b9\u6570\u8fd8\u662f\u7b26\u5408\u7279\u5b9a\u5206\u5e03\u7684\u968f\u673a\u6570\uff0crandom\u6a21\u5757\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u6765\u6ee1\u8db3\u5404\u79cd\u9700\u6c42\u3002\u901a\u8fc7\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u51fd\u6570\u7684\u7528\u6cd5\uff0c\u53ef\u4ee5\u5728\u7f16\u7a0b\u4e2d\u7075\u6d3b\u5730\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u4e86\u89e3\u548c\u4f7f\u7528Python\u751f\u6210\u968f\u673a\u6570\u6709\u6240\u5e2e\u52a9\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u5e2e\u52a9\uff0c\u8bf7\u968f\u65f6\u63d0\u51fa\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\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u6765\u751f\u6210\u968f\u673a\u6570\u5b57\u3002\u901a\u8fc7<code>random.randint(a, b)<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6307\u5b9a\u7684\u8303\u56f4<code>a<\/code>\u5230<code>b<\/code>\u4e4b\u95f4\uff08\u5305\u62ec<code>a<\/code>\u548c<code>b<\/code>\uff09\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6574\u6570\u3002\u4f8b\u5982\uff0c<code>random.randint(1, 10)<\/code>\u5c06\u968f\u673a\u8f93\u51fa1\u523010\u4e4b\u95f4\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u751f\u6210\u6d6e\u70b9\u6570\u7684\u968f\u673a\u6570\u5b57\uff1f<\/strong><br \/>\u9664\u4e86\u751f\u6210\u6574\u6570\uff0cPython\u7684<code>random<\/code>\u6a21\u5757\u8fd8\u5141\u8bb8\u60a8\u751f\u6210\u6d6e\u70b9\u6570\u3002\u4f7f\u7528<code>random.uniform(a, b)<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6307\u5b9a\u7684\u8303\u56f4\u5185\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\u3002\u8fd9\u4e00\u8303\u56f4\u540c\u6837\u662f\u5305\u542b<code>a<\/code>\u548c<code>b<\/code>\u7684\u3002\u4f8b\u5982\uff0c<code>random.uniform(1.5, 5.5)<\/code>\u5c06\u8fd4\u56de1.5\u52305.5\u4e4b\u95f4\u7684\u4e00\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u6570\u5b57\u662f\u552f\u4e00\u7684\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u751f\u6210\u4e00\u7ec4\u552f\u4e00\u7684\u968f\u673a\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.sample()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u5141\u8bb8\u60a8\u4ece\u4e00\u4e2a\u6307\u5b9a\u7684\u8303\u56f4\u4e2d\u968f\u673a\u9009\u62e9\u591a\u4e2a\u4e0d\u91cd\u590d\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff0c<code>random.sample(range(1, 100), 10)<\/code>\u5c06\u4ece1\u523099\u4e2d\u968f\u673a\u9009\u62e910\u4e2a\u552f\u4e00\u7684\u6570\u5b57\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u6570\u5b57\u4e0d\u4f1a\u91cd\u590d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528random\u6a21\u5757\u6765\u968f\u673a\u8f93\u51fa\u4e00\u4e2a\u6570\u5b57\u3001random\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u751f\u6210\u968f\u673a\u6570\u3001\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":1102138,"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\/1102128"}],"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=1102128"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102128\/revisions"}],"predecessor-version":[{"id":1102139,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102128\/revisions\/1102139"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1102138"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1102128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1102128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1102128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}