{"id":1082996,"date":"2025-01-08T12:53:53","date_gmt":"2025-01-08T04:53:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1082996.html"},"modified":"2025-01-08T12:53:55","modified_gmt":"2025-01-08T04:53:55","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e7%94%9f%e6%88%9010%e4%b8%aa%e6%95%b0%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1082996.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u751f\u621010\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\/24193808\/86c04c0d-10de-48b7-bf70-5e82b0594664.webp\" alt=\"python\u5982\u4f55\u968f\u673a\u751f\u621010\u4e2a\u6570\u5b57\" \/><\/p>\n<p><p> <strong>Python\u968f\u673a\u751f\u621010\u4e2a\u6570\u5b57\u7684\u65b9\u6cd5<\/strong>\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u3001<code>numpy<\/code>\u5e93\u7b49\u65b9\u6cd5\uff0c\u5176\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\u6709<strong>random.sample()\u3001random.randint()\u3001numpy.random.randint()<\/strong>\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>random.sample()\u65b9\u6cd5<\/strong>\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u4ece\u6307\u5b9a\u7684\u5e8f\u5217\u4e2d\u968f\u673a\u62bd\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u4e0d\u4f1a\u91cd\u590d\u3002\u5b83\u7684\u57fa\u672c\u7528\u6cd5\u662f<code>random.sample(population, k)<\/code>\uff0c\u5176\u4e2d<code>population<\/code>\u662f\u8981\u62bd\u53d6\u5143\u7d20\u7684\u5e8f\u5217\uff0c<code>k<\/code>\u662f\u8981\u62bd\u53d6\u7684\u6570\u91cf\u3002\u5047\u5982\u6211\u4eec\u60f3\u968f\u673a\u751f\u621010\u4e2a\u57280\u5230100\u4e4b\u95f4\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_numbers = random.sample(range(101), 10)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001random\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>random<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u3002\u4e0b\u9762\u6211\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u751f\u6210\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001random.randint()<\/h4>\n<\/p>\n<p><p><code>random.randint(a, b)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210[a, b]\u4e4b\u95f4\u7684\u4e00\u4e2a\u6574\u6570\u3002\u82e5\u8981\u751f\u621010\u4e2a\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u5229\u7528\u5faa\u73af\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_numbers = [random.randint(0, 100) for _ in range(10)]<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001random.uniform()<\/h4>\n<\/p>\n<p><p><code>random.uniform(a, b)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210[a, b]\u4e4b\u95f4\u7684\u4e00\u4e2a\u6d6e\u70b9\u6570\u3002\u82e5\u8981\u751f\u621010\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u5229\u7528\u5faa\u73af\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_numbers = [random.uniform(0, 100) for _ in range(10)]<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001random.sample()<\/h4>\n<\/p>\n<p><p>\u5982\u524d\u6587\u6240\u8ff0\uff0c<code>random.sample()<\/code>\u53ef\u4ee5\u4ece\u6307\u5b9a\u5e8f\u5217\u4e2d\u62bd\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u4e0d\u4f1a\u91cd\u590d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_numbers = random.sample(range(101), 10)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001numpy\u5e93<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u3002\u4e0b\u9762\u6211\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>numpy<\/code>\u5e93\u751f\u6210\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001numpy.random.randint()<\/h4>\n<\/p>\n<p><p><code>numpy.random.randint(low, high=None, size=None, dtype=&#39;l&#39;)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u6570\u7ec4\u3002\u82e5\u8981\u751f\u621010\u4e2a\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>random_numbers = np.random.randint(0, 101, size=10)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001numpy.random.uniform()<\/h4>\n<\/p>\n<p><p><code>numpy.random.uniform(low=0.0, high=1.0, size=None)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4\u3002\u82e5\u8981\u751f\u621010\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>random_numbers = np.random.uniform(0, 100, size=10)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001secrets\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>secrets<\/code>\u6a21\u5757\u662f\u7528\u4e8e\u751f\u6210\u5b89\u5168\u968f\u673a\u6570\u7684\u6a21\u5757\uff0c\u9002\u7528\u4e8e\u5bc6\u7801\u5b66\u76f8\u5173\u7684\u7528\u9014\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>secrets<\/code>\u6a21\u5757\u751f\u6210\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001secrets.randbelow()<\/h4>\n<\/p>\n<p><p><code>secrets.randbelow(n)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u62100\u5230n-1\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002\u82e5\u8981\u751f\u621010\u4e2a\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u5229\u7528\u5faa\u73af\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>random_numbers = [secrets.randbelow(101) for _ in range(10)]<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001secrets.choice()<\/h4>\n<\/p>\n<p><p><code>secrets.choice(sequence)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4ece\u6307\u5b9a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002\u82e5\u8981\u751f\u621010\u4e2a\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u5229\u7528\u5217\u8868\u548c\u5faa\u73af\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>random_numbers = [secrets.choice(range(101)) for _ in range(10)]<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u968f\u673a\u751f\u621010\u4e2a\u6570\u5b57<\/strong>\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<code>random<\/code>\u6a21\u5757\u9002\u5408\u4e00\u822c\u7528\u9014\uff0c<code>numpy<\/code>\u5e93\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c<code>secrets<\/code>\u6a21\u5757\u9002\u5408\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u751f\u6210\u7684\u968f\u673a\u6570\u8303\u56f4\u548c\u6570\u91cf\uff0c\u4ee5\u6ee1\u8db3\u5b9e\u9645\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5728Python\u4e2d\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u968f\u673a\u6570\uff0c\u4ece\u800c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u7684\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u751f\u6210\u4e00\u7ec4\u968f\u673a\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u6765\u751f\u6210\u968f\u673a\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>random.randint(a, b)<\/code>\u53ef\u4ee5\u751f\u6210\u8303\u56f4\u5728a\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002\u5982\u679c\u60f3\u751f\u621010\u4e2a\u968f\u673a\u6570\u5b57\uff0c\u53ef\u4ee5\u5c06\u5176\u653e\u5165\u4e00\u4e2a\u5faa\u73af\u6216\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import random\n\nrandom_numbers = [random.randint(1, 100) for _ in range(10)]\nprint(random_numbers)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u751f\u621010\u4e2a\u57281\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u6570\u5b57\u4e0d\u91cd\u590d\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u751f\u6210\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.sample()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5141\u8bb8\u60a8\u4ece\u4e00\u4e2a\u8303\u56f4\u4e2d\u9009\u62e9\u4e0d\u91cd\u590d\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u751f\u621010\u4e2a1\u5230100\u4e4b\u95f4\u7684\u4e0d\u91cd\u590d\u968f\u673a\u6570\u5b57\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-python\">unique_random_numbers = random.sample(range(1, 101), 10)\nprint(unique_random_numbers)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u786e\u4fdd\u751f\u6210\u7684\u6570\u5b57\u90fd\u662f\u552f\u4e00\u7684\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u6574\u751f\u6210\u968f\u673a\u6570\u5b57\u7684\u8303\u56f4\uff1f<\/strong><br \/>\u5728\u751f\u6210\u968f\u673a\u6570\u5b57\u65f6\uff0c\u60a8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6570\u5b57\u7684\u8303\u56f4\u3002\u53ea\u9700\u5728<code>random.randint(a, b)<\/code>\u6216<code>random.sample()<\/code>\u4e2d\u8c03\u6574\u53c2\u6570<code>a<\/code>\u548c<code>b<\/code>\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u751f\u621010\u4e2a\u8303\u56f4\u572850\u5230150\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-python\">custom_random_numbers = [random.randint(50, 150) for _ in range(10)]\nprint(custom_random_numbers)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u60a8\u5c31\u80fd\u5f97\u5230\u7b26\u5408\u9700\u6c42\u7684\u968f\u673a\u6570\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u968f\u673a\u751f\u621010\u4e2a\u6570\u5b57\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u3001numpy\u5e93\u7b49\u65b9\u6cd5\uff0c\u5176\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1083005,"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\/1082996"}],"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=1082996"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082996\/revisions"}],"predecessor-version":[{"id":1083008,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082996\/revisions\/1083008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1083005"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1082996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1082996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1082996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}