{"id":1086489,"date":"2025-01-08T13:26:11","date_gmt":"2025-01-08T05:26:11","guid":{"rendered":""},"modified":"2025-01-08T13:26:14","modified_gmt":"2025-01-08T05:26:14","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e7%94%9f%e6%88%90100%e4%b8%aa%e6%95%b0%e5%ad%97-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1086489.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u751f\u6210100\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\/24195855\/0162dfe8-2ef7-4d5b-a96a-40337f26a594.webp\" alt=\"python\u5982\u4f55\u968f\u673a\u751f\u6210100\u4e2a\u6570\u5b57\" \/><\/p>\n<p><p> <strong>Python\u968f\u673a\u751f\u6210100\u4e2a\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528random\u6a21\u5757\u7684randint\u3001uniform\u3001sample\u3001choices\u7b49\u51fd\u6570\u5b9e\u73b0\u3001randint\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6574\u6570\u3001uniform\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6d6e\u70b9\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6574\u6570<\/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\u4e2drandint\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528randint\u751f\u6210\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_numbers = [random.randint(1, 100) for _ in range(100)]<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>randint\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u968f\u673a\u6574\u6570\u7684\u4e0b\u9650\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u968f\u673a\u6574\u6570\u7684\u4e0a\u9650\uff08\u5305\u62ec\u4e0a\u9650\uff09\u3002\u8fd9\u6837\u53ef\u4ee5\u751f\u6210100\u4e2a1\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528randrange\u751f\u6210\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><p>randrange\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6574\u6570\uff0c\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u6b65\u957f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_numbers = [random.randrange(1, 101) for _ in range(100)]<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>randrange\u51fd\u6570\u4e0erandint\u7c7b\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b65\u957f\u751f\u6210\u7279\u5b9a\u95f4\u9694\u7684\u968f\u673a\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u751f\u6210\u968f\u673a\u6574\u6570\uff0crandom\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982uniform\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528uniform\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">random_numbers = [random.uniform(1.0, 100.0) for _ in range(100)]<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>uniform\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u968f\u673a\u6d6e\u70b9\u6570\u7684\u4e0b\u9650\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u968f\u673a\u6d6e\u70b9\u6570\u7684\u4e0a\u9650\u3002\u8fd9\u6837\u53ef\u4ee5\u751f\u6210100\u4e2a1.0\u5230100.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528random\u51fd\u6570\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><p>random\u51fd\u6570\u53ef\u4ee5\u751f\u62100.0\u52301.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_numbers = [random.random() for _ in range(100)]<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>random\u51fd\u6570\u4e0d\u9700\u8981\u53c2\u6570\uff0c\u53ef\u4ee5\u751f\u62100.0\u52301.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u5c06\u5176\u8f6c\u6362\u4e3a\u7279\u5b9a\u8303\u56f4\u7684\u6d6e\u70b9\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528sample\u51fd\u6570\u751f\u6210\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u751f\u6210\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528sample\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_numbers = random.sample(range(1, 101), 100)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>sample\u51fd\u6570\u53ef\u4ee5\u4ece\u6307\u5b9a\u8303\u56f4\u5185\u968f\u673a\u62bd\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u4e0d\u91cd\u590d\u5143\u7d20\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8303\u56f4\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u62bd\u53d6\u7684\u6570\u91cf\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528choices\u51fd\u6570\u751f\u6210\u91cd\u590d\u7684\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u751f\u6210\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528choices\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_numbers = random.choices(range(1, 101), k=100)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>choices\u51fd\u6570\u53ef\u4ee5\u4ece\u6307\u5b9a\u8303\u56f4\u5185\u968f\u673a\u62bd\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\uff08\u5141\u8bb8\u91cd\u590d\uff09\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8303\u56f4\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u62bd\u53d6\u7684\u6570\u91cf\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Numpy\u751f\u6210\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86random\u6a21\u5757\uff0cNumpy\u5e93\u4e5f\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u3002Numpy\u7684random\u6a21\u5757\u529f\u80fd\u66f4\u4e3a\u5f3a\u5927\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u7684\u968f\u673a\u6570\u751f\u6210\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528numpy.random.randint\u751f\u6210\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>random_numbers = np.random.randint(1, 101, size=100)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>numpy.random.randint\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e0b\u9650\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e0a\u9650\uff08\u4e0d\u5305\u62ec\uff09\uff0csize\u53c2\u6570\u6307\u5b9a\u751f\u6210\u968f\u673a\u6570\u7684\u6570\u91cf\u3002<\/strong><\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528numpy.random.uniform\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">random_numbers = np.random.uniform(1.0, 100.0, size=100)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>numpy.random.uniform\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e0b\u9650\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e0a\u9650\uff0csize\u53c2\u6570\u6307\u5b9a\u751f\u6210\u968f\u673a\u6570\u7684\u6570\u91cf\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516d\u3001\u751f\u6210\u968f\u673a\u6570\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u968f\u673a\u6570\u751f\u6210\u5728\u5f88\u591a\u5e94\u7528\u573a\u666f\u4e2d\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u6a21\u62df<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u751f\u6210\u968f\u673a\u6570\u6765\u6a21\u62df\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u6570\u751f\u6210\u5668\u6765\u6a21\u62df\u80a1\u7968\u4ef7\u683c\u3001\u5929\u6c14\u6570\u636e\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2. \u968f\u673a\u62bd\u6837<\/h4>\n<\/p>\n<p><p>\u5728\u7edf\u8ba1\u5b66\u4e2d\uff0c\u968f\u673a\u62bd\u6837\u662f\u4e00\u79cd\u91cd\u8981\u7684\u6280\u672f\u3002\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u6570\u751f\u6210\u5668\u4ece\u6570\u636e\u96c6\u4e2d\u968f\u673a\u62bd\u53d6\u6837\u672c\uff0c\u4ee5\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>3. \u6e38\u620f\u5f00\u53d1<\/h4>\n<\/p>\n<p><p>\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c\u968f\u673a\u6570\u751f\u6210\u5668\u53ef\u4ee5\u7528\u6765\u751f\u6210\u968f\u673a\u5730\u56fe\u3001\u968f\u673a\u654c\u4eba\u7b49\uff0c\u589e\u52a0\u6e38\u620f\u7684\u8da3\u5473\u6027\u548c\u6311\u6218\u6027\u3002<\/p>\n<\/p>\n<p><h4>4. \u5bc6\u7801\u751f\u6210<\/h4>\n<\/p>\n<p><p>\u5728\u4fe1\u606f\u5b89\u5168\u4e2d\uff0c\u968f\u673a\u6570\u751f\u6210\u5668\u53ef\u4ee5\u7528\u6765\u751f\u6210\u968f\u673a\u5bc6\u7801\uff0c\u63d0\u9ad8\u5bc6\u7801\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u751f\u6210100\u4e2a\u968f\u673a\u6570\u3002\u53ef\u4ee5\u4f7f\u7528random\u6a21\u5757\u7684randint\u3001uniform\u3001sample\u3001choices\u7b49\u51fd\u6570\u751f\u6210\u968f\u673a\u6574\u6570\u548c\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Numpy\u5e93\u751f\u6210\u5927\u89c4\u6a21\u7684\u968f\u673a\u6570\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u6709\u4efb\u4f55\u7591\u95ee\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\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\u5b57\u7684\u8303\u56f4\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u751f\u6210\u7279\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\u5b57\u3002\u901a\u8fc7<code>random.randint(a, b)<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u751f\u6210\u5728<code>a<\/code>\u5230<code>b<\/code>\uff08\u5305\u62ec<code>a<\/code>\u548c<code>b<\/code>\uff09\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002\u5982\u679c\u60a8\u5e0c\u671b\u751f\u6210\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\uff0c\u5b83\u4f1a\u5728<code>a<\/code>\u548c<code>b<\/code>\u4e4b\u95f4\u751f\u6210\u968f\u673a\u7684\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u751f\u6210\u7684\u968f\u673a\u6570\u5b57\u662f\u5426\u53ef\u4ee5\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u751f\u6210\u7684\u968f\u673a\u6570\u5b57\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>[random.randint(1, 100) for _ in range(100)]<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b100\u4e2a\u968f\u673a\u6574\u6570\u7684\u5217\u8868\u3002\u8fd9\u6837\u60a8\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u548c\u8bbf\u95ee\u8fd9\u4e9b\u6570\u5b57\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>\u51fd\u6570\u3002\u6b64\u51fd\u6570\u5141\u8bb8\u60a8\u6307\u5b9a\u4e00\u4e2a\u8303\u56f4\u548c\u6240\u9700\u7684\u6837\u672c\u6570\u91cf\uff0c\u4f8b\u5982<code>random.sample(range(1, 101), 100)<\/code>\uff0c\u8fd9\u5c06\u751f\u62101\u5230100\u4e4b\u95f4\u7684100\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\u5b57\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u6570\u5b57\u5728\u6307\u5b9a\u8303\u56f4\u5185\u4e14\u4e0d\u91cd\u590d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u968f\u673a\u751f\u6210100\u4e2a\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528random\u6a21\u5757\u7684randint\u3001uniform\u3001sample\u3001c [&hellip;]","protected":false},"author":3,"featured_media":1086498,"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\/1086489"}],"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=1086489"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086489\/revisions"}],"predecessor-version":[{"id":1086502,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086489\/revisions\/1086502"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1086498"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1086489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1086489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1086489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}