{"id":1124102,"date":"2025-01-08T19:38:24","date_gmt":"2025-01-08T11:38:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1124102.html"},"modified":"2025-01-08T19:38:26","modified_gmt":"2025-01-08T11:38:26","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e4%ba%a7%e7%94%9f100%e4%b8%aa%e4%b8%a4%e4%bd%8d%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1124102.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u4ea7\u751f100\u4e2a\u4e24\u4f4d\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085309\/7d92a87b-ee80-4c29-b8f6-f2466c77cb23.webp\" alt=\"python\u5982\u4f55\u968f\u673a\u4ea7\u751f100\u4e2a\u4e24\u4f4d\u6570\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u968f\u673a\u4ea7\u751f100\u4e2a\u4e24\u4f4d\u6570\uff0c\u4f8b\u5982\u4f7f\u7528random\u6a21\u5757\u4e2d\u7684randint\u51fd\u6570\u3001sample\u51fd\u6570\u4ee5\u53ca\u5176\u4ed6\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528random\u6a21\u5757\u7684randint\u51fd\u6570<\/strong>\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\u3002randint\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u901a\u8fc7\u5faa\u73af\u751f\u6210100\u4e2a\u4e24\u4f4d\u6570\u5e76\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528random\u6a21\u5757\u7684sample\u51fd\u6570<\/strong>\u3002sample\u51fd\u6570\u53ef\u4ee5\u4ece\u4e00\u4e2a\u8303\u56f4\u5185\u4e0d\u91cd\u590d\u5730\u62bd\u53d6\u6837\u672c\uff0c\u5c3d\u7ba1\u8fd9\u4e2a\u65b9\u6cd5\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u5904\u7406\u6765\u786e\u4fdd\u751f\u6210\u7684\u6570\u5b57\u90fd\u5728\u6307\u5b9a\u8303\u56f4\u5185\u3002<\/p>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528numpy\u5e93\u7684random\u51fd\u6570<\/strong>\u3002numpy\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u5b83\u7684random\u6a21\u5757\u4e5f\u80fd\u65b9\u4fbf\u5730\u751f\u6210\u6240\u9700\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528random\u6a21\u5757\u7684randint\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684random\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u6cd5\u6765\u751f\u6210\u968f\u673a\u6570\uff0c\u5176\u4e2drandint\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u3002\u5b83\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528randint\u51fd\u6570\u6765\u751f\u6210100\u4e2a\u4e24\u4f4d\u6570\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def generate_random_numbers():<\/p>\n<p>    random_numbers = []<\/p>\n<p>    for _ in range(100):<\/p>\n<p>        random_numbers.append(random.randint(10, 99))<\/p>\n<p>    return random_numbers<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u5e76\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>random_numbers = generate_random_numbers()<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86random\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3agenerate_random_numbers\u7684\u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2afor\u5faa\u73af\u6765\u751f\u6210100\u4e2a\u4e24\u4f4d\u6570\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u6253\u5370\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528random\u6a21\u5757\u7684sample\u51fd\u6570<\/h3>\n<\/p>\n<p><p>sample\u51fd\u6570\u53ef\u4ee5\u4ece\u4e00\u4e2a\u6307\u5b9a\u7684\u8303\u56f4\u5185\u62bd\u53d6\u4e0d\u91cd\u590d\u7684\u6837\u672c\u3002\u5c3d\u7ba1\u5b83\u901a\u5e38\u7528\u4e8e\u62bd\u53d6\u4e0d\u91cd\u590d\u7684\u6837\u672c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6837\u672c\u8303\u56f4\u548c\u6837\u672c\u6570\u91cf\u6765\u751f\u6210\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def generate_random_numbers():<\/p>\n<p>    range_of_numbers = list(range(10, 100))  # \u751f\u621010\u523099\u7684\u6240\u6709\u4e24\u4f4d\u6570<\/p>\n<p>    random_numbers = random.sample(range_of_numbers, 100)<\/p>\n<p>    return random_numbers<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u5e76\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>random_numbers = generate_random_numbers()<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u6240\u6709\u4e24\u4f4d\u6570\u7684\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528sample\u51fd\u6570\u4ece\u4e2d\u62bd\u53d6100\u4e2a\u6570\u5b57\u3002\u7531\u4e8esample\u51fd\u6570\u751f\u6210\u7684\u6570\u5b57\u662f\u552f\u4e00\u7684\uff0c\u5982\u679c\u9700\u8981\u91cd\u590d\u7684\u6570\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u591a\u6b21\u8fd0\u884c\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528numpy\u5e93\u7684random\u51fd\u6570<\/h3>\n<\/p>\n<p><p>numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u5b83\u7684random\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u3002numpy\u7684random.randint\u51fd\u6570\u7c7b\u4f3c\u4e8ePython\u5185\u7f6e\u7684random.randint\u51fd\u6570\uff0c\u4f46\u5b83\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def generate_random_numbers():<\/p>\n<p>    random_numbers = np.random.randint(10, 100, 100)<\/p>\n<p>    return random_numbers<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u5e76\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>random_numbers = generate_random_numbers()<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528numpy\u7684random.randint\u51fd\u6570\u4e00\u6b21\u6027\u751f\u6210100\u4e2a\u4e24\u4f4d\u6570\u3002\u7531\u4e8enumpy\u7684random\u6a21\u5757\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u66f4\u9ad8\u6548\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u751f\u6210100\u4e2a\u4e24\u4f4d\u6570\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>randint\u51fd\u6570<\/strong>\uff1a\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>sample\u51fd\u6570<\/strong>\uff1a\u9002\u5408\u9700\u8981\u4ece\u4e00\u4e2a\u8303\u56f4\u5185\u62bd\u53d6\u4e0d\u91cd\u590d\u7684\u6837\u672c\u3002<\/li>\n<li><strong>numpy\u7684random\u51fd\u6570<\/strong>\uff1a\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u751f\u6210\u968f\u673a\u6570\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210100\u4e2a\u968f\u673a\u7684\u4e24\u4f4d\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u6765\u751f\u6210\u968f\u673a\u6570\u3002\u5177\u4f53\u6b65\u9aa4\u662f\u5bfc\u5165<code>random<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>randint<\/code>\u51fd\u6570\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import random\n\nrandom_numbers = [random.randint(10, 99) for _ in range(100)]\nprint(random_numbers)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u751f\u6210100\u4e2a\u572810\u523099\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5f62\u6210\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<p><strong>\u751f\u6210\u7684\u968f\u673a\u4e24\u4f4d\u6570\u53ef\u4ee5\u91cd\u590d\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u968f\u673a\u6570\u53ef\u4ee5\u91cd\u590d\u3002\u6bcf\u6b21\u8c03\u7528<code>randint<\/code>\u51fd\u6570\u65f6\uff0c\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u968f\u673a\u6570\uff0c\u56e0\u6b64\u5217\u8868\u4e2d\u53ef\u80fd\u4f1a\u5305\u542b\u76f8\u540c\u7684\u6570\u5b57\u3002\u5982\u679c\u60f3\u8981\u751f\u6210\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.sample<\/code>\u51fd\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u6570\u5728\u6307\u5b9a\u8303\u56f4\u5185\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>randint<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8303\u56f4\u7684\u4e0a\u4e0b\u9650\u6765\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u6570\u5728\u6240\u9700\u7684\u8303\u56f4\u5185\u3002\u4f8b\u5982\uff0c<code>random.randint(10, 99)<\/code>\u786e\u4fdd\u751f\u6210\u7684\u662f\u4e24\u4f4d\u6570\u3002\u786e\u4fdd\u4e0a\u4e0b\u9650\u8bbe\u7f6e\u6b63\u786e\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u751f\u6210\u4e09\u4f4d\u6570\u6216\u5176\u4ed6\u4e0d\u7b26\u5408\u8981\u6c42\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u751f\u6210\u7684\u968f\u673a\u6570\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u5c06\u751f\u6210\u7684\u968f\u673a\u6570\u4fdd\u5b58\u5230\u6587\u672c\u6587\u4ef6\u4e2d\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;random_numbers.txt&#39;, &#39;w&#39;) as f:\n    for number in random_numbers:\n        f.write(f&quot;{number}\\n&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u628a\u6bcf\u4e2a\u751f\u6210\u7684\u968f\u673a\u6570\u5199\u5165\u4e00\u4e2a\u540d\u4e3a<code>random_numbers.txt<\/code>\u7684\u6587\u4ef6\u4e2d\uff0c\u6bcf\u4e2a\u6570\u5b57\u5360\u4e00\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u968f\u673a\u4ea7\u751f100\u4e2a\u4e24\u4f4d\u6570\uff0c\u4f8b\u5982\u4f7f\u7528random\u6a21\u5757\u4e2d\u7684randint\u51fd\u6570\u3001samp [&hellip;]","protected":false},"author":3,"featured_media":1124105,"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\/1124102"}],"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=1124102"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124102\/revisions"}],"predecessor-version":[{"id":1124109,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124102\/revisions\/1124109"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1124105"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1124102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1124102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1124102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}