{"id":1035499,"date":"2024-12-31T11:59:19","date_gmt":"2024-12-31T03:59:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1035499.html"},"modified":"2024-12-31T11:59:22","modified_gmt":"2024-12-31T03:59:22","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e9%9a%8f%e6%9c%ba%e7%94%9f%e6%88%90%e7%9a%84%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1035499.html","title":{"rendered":"python\u5982\u4f55\u5c06\u968f\u673a\u751f\u6210\u7684\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/bb07b892-5555-4d26-93be-175eab8a1b8f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u968f\u673a\u751f\u6210\u7684\u6570\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u6574\u6570\u3001\u5c0f\u6570\u6216\u4ece\u6307\u5b9a\u8303\u56f4\u5185\u9009\u62e9\u968f\u673a\u6570\u3001\u751f\u6210\u968f\u673a\u5e8f\u5217\u7b49\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ecrandom.randint()\u3001random.random()\u3001random.uniform()\u3001random.choice()\u7b49\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1arandom.randint()\u751f\u6210\u968f\u673a\u6574\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u968f\u673a\u6574\u6570\u751f\u6210<\/h3>\n<\/p>\n<p><p>Python\u7684random\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u5176\u4e2drandom.randint(a, b)\u53ef\u4ee5\u751f\u6210[a, b]\u533a\u95f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u62101\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_integer = random.randint(1, 100)<\/p>\n<p>print(random_integer)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><br \/>random.randint()\u65b9\u6cd5\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u8868\u793a\u751f\u6210\u968f\u673a\u6574\u6570\u7684\u8303\u56f4\uff08\u5305\u62ec\u4e24\u4e2a\u7aef\u70b9\uff09\u3002\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0crandom.randint(1, 100)\u4f1a\u751f\u6210\u4e00\u4e2a1\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u968f\u673a\u6d6e\u70b9\u6570\u751f\u6210<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u751f\u6210\u968f\u673a\u6574\u6570\u5916\uff0crandom\u6a21\u5757\u8fd8\u53ef\u4ee5\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u3002random.random()\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210[0.0, 1.0)\u533a\u95f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0crandom.uniform(a, b)\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210[a, b]\u533a\u95f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u62100.0\u52301.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float = random.random()<\/p>\n<p>print(random_float)<\/p>\n<h2><strong>\u751f\u62101.5\u52303.5\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_uniform = random.uniform(1.5, 3.5)<\/p>\n<p>print(random_uniform)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9<\/h3>\n<\/p>\n<p><p>random.choice(seq)\u65b9\u6cd5\u53ef\u4ee5\u4ece\u4e00\u4e2a\u975e\u7a7a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>random_choice = random.choice(my_list)<\/p>\n<p>print(random_choice)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u751f\u6210\u968f\u673a\u5e8f\u5217<\/h3>\n<\/p>\n<p><p>random.sample(population, k)\u65b9\u6cd5\u53ef\u4ee5\u4ece\u6307\u5b9a\u7684\u5e8f\u5217\u4e2d\u751f\u6210\u957f\u5ea6\u4e3ak\u7684\u65b0\u5e8f\u5217\uff0crandom.shuffle(x)\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u5e8f\u5217\u8fdb\u884c\u968f\u673a\u6253\u4e71\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e93\u4e2a\u5143\u7d20\u751f\u6210\u65b0\u5217\u8868<\/strong><\/h2>\n<p>random_sample = random.sample(my_list, 3)<\/p>\n<p>print(random_sample)<\/p>\n<h2><strong>\u5bf9\u5217\u8868\u8fdb\u884c\u968f\u673a\u6253\u4e71<\/strong><\/h2>\n<p>random.shuffle(my_list)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u8bc1\u968f\u673a\u6570\u751f\u6210\u7684\u53ef\u91cd\u590d\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528random.seed()\u65b9\u6cd5\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50<\/strong><\/h2>\n<p>random.seed(10)<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u6570<\/strong><\/h2>\n<p>print(random.randint(1, 100))<\/p>\n<p>print(random.random())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u968f\u673a\u6570\u751f\u6210\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u5206\u6790\u4e2d\u53ef\u4ee5\u7528\u4e8e\u62bd\u6837\uff0c\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u968f\u673a\u4e8b\u4ef6\uff0c\u5728\u5bc6\u7801\u5b66\u4e2d\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u968f\u673a\u5bc6\u94a5\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b1\uff1a\u6570\u636e\u62bd\u6837<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u968f\u673a\u62bd\u6837\u5e38\u7528\u4e8e\u4ece\u5927\u89c4\u6a21\u6570\u636e\u96c6\u4e2d\u62bd\u53d6\u6837\u672c\uff0c\u4ee5\u4fbf\u5728\u6837\u672c\u4e0a\u8fdb\u884c\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528random.sample()\u8fdb\u884c\u6570\u636e\u62bd\u6837\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = list(range(1, 101))<\/p>\n<h2><strong>\u4ece\u6570\u636e\u96c6\u4e2d\u968f\u673a\u62bd\u53d610\u4e2a\u6837\u672c<\/strong><\/h2>\n<p>sample = random.sample(data, 10)<\/p>\n<p>print(sample)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b2\uff1a\u751f\u6210\u968f\u673a\u4e8b\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c\u751f\u6210\u968f\u673a\u4e8b\u4ef6\u662f\u5e38\u89c1\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5728\u89d2\u8272\u626e\u6f14\u6e38\u620f\u4e2d\uff0c\u53ef\u4ee5\u968f\u673a\u751f\u6210\u602a\u7269\u7684\u51fa\u73b0\u4f4d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528random.randint()\u751f\u6210\u968f\u673a\u4f4d\u7f6e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u5b9a\u4e49\u5730\u56fe\u5927\u5c0f<\/strong><\/h2>\n<p>map_width = 100<\/p>\n<p>map_height = 100<\/p>\n<h2><strong>\u751f\u6210\u602a\u7269\u7684\u968f\u673a\u4f4d\u7f6e<\/strong><\/h2>\n<p>monster_x = random.randint(0, map_width - 1)<\/p>\n<p>monster_y = random.randint(0, map_height - 1)<\/p>\n<p>print(f&quot;Monster position: ({monster_x}, {monster_y})&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b3\uff1a\u751f\u6210\u968f\u673a\u5bc6\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u5bc6\u7801\u5b66\u4e2d\uff0c\u751f\u6210\u968f\u673a\u5bc6\u7801\u662f\u5e38\u89c1\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528random.choice()\u751f\u6210\u968f\u673a\u5bc6\u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import string<\/p>\n<h2><strong>\u5b9a\u4e49\u5bc6\u7801\u957f\u5ea6<\/strong><\/h2>\n<p>password_length = 12<\/p>\n<h2><strong>\u5b9a\u4e49\u53ef\u9009\u5b57\u7b26\u96c6<\/strong><\/h2>\n<p>characters = string.ascii_letters + string.digits + string.punctuation<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u5bc6\u7801<\/strong><\/h2>\n<p>password = &#39;&#39;.join(random.choice(characters) for _ in range(password_length))<\/p>\n<p>print(f&quot;Generated password: {password}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u7684random\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\uff0c\u5305\u62ec\u751f\u6210\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u4ece\u5e8f\u5217\u4e2d\u9009\u62e9\u5143\u7d20\u3001\u751f\u6210\u968f\u673a\u5e8f\u5217\u7b49\u3002\u901a\u8fc7\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u968f\u673a\u6570\u751f\u6210\u7684\u53ef\u91cd\u590d\u6027\u3002\u968f\u673a\u6570\u751f\u6210\u5728\u6570\u636e\u5206\u6790\u3001\u6e38\u620f\u5f00\u53d1\u3001\u5bc6\u7801\u5b66\u7b49\u9886\u57df\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u89e3\u51b3\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u95ee\u9898\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\u901a\u8fc7<code>random.random()<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u6d6e\u52a8\u968f\u673a\u6570\uff0c\u800c<code>random.randint(a, b)<\/code>\u5219\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5728a\u548cb\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5404\u79cd\u968f\u673a\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u751f\u6210\u7684\u968f\u673a\u6570\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u4e3a\u4e86\u5c06\u751f\u6210\u7684\u968f\u673a\u6570\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u3002\u9996\u5148\uff0c\u751f\u6210\u968f\u673a\u6570\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u968f\u673a\u6570\u5199\u5165\u6587\u4ef6\u3002\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u4e0d\u8981\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5b89\u5168\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u591a\u4e2a\u968f\u673a\u6570\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<code>[random.randint(a, b) for _ in range(n)]<\/code>\u53ef\u4ee5\u751f\u6210n\u4e2a\u5728a\u548cb\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9ad8\u6548\u4e14\u7b80\u6d01\uff0c\u9002\u5408\u5728\u9700\u8981\u5927\u91cf\u968f\u673a\u6570\u7684\u573a\u666f\u4e2d\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u6574\u6570\u3001\u5c0f\u6570\u6216\u4ece\u6307\u5b9a\u8303\u56f4\u5185\u9009\u62e9\u968f\u673a\u6570\u3001\u751f\u6210\u968f [&hellip;]","protected":false},"author":3,"featured_media":1035511,"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\/1035499"}],"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=1035499"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035499\/revisions"}],"predecessor-version":[{"id":1035515,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035499\/revisions\/1035515"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1035511"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1035499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1035499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1035499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}