{"id":1093955,"date":"2025-01-08T14:36:21","date_gmt":"2025-01-08T06:36:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1093955.html"},"modified":"2025-01-08T14:36:24","modified_gmt":"2025-01-08T06:36:24","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e4%ba%a7%e7%94%9f%e4%b8%80%e5%a0%86%e6%95%b0%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1093955.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u4ea7\u751f\u4e00\u5806\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210313\/6a5eacbb-4b63-48b9-b7eb-bb964692a10f.webp\" alt=\"python\u5982\u4f55\u968f\u673a\u4ea7\u751f\u4e00\u5806\u6570\u5b57\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u968f\u673a\u4ea7\u751f\u4e00\u5806\u6570\u5b57\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\uff0c\u901a\u8fc7<strong>\u4f7f\u7528random\u6a21\u5757\u3001\u751f\u6210\u968f\u673a\u6574\u6570\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u3001\u751f\u6210\u968f\u673a\u5e8f\u5217<\/strong>\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u6c42\u751f\u6210\u4e0d\u540c\u7c7b\u578b\u7684\u968f\u673a\u6570\u5b57\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff1a\u4f7f\u7528<code>random.randint<\/code>\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u8981\u751f\u6210\u4e00\u5806\u968f\u673a\u6574\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>random.randint<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5141\u8bb8\u4f60\u6307\u5b9a\u4e00\u4e2a\u8303\u56f4\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u5728\u8fd9\u4e2a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u5305\u542b10\u4e2a\u968f\u673a\u6574\u6570\u7684\u5217\u8868\uff0c\u8303\u56f4\u57281\u5230100\u4e4b\u95f4<\/strong><\/h2>\n<p>random_numbers = [random.randint(1, 100) for _ in range(10)]<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u5757\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u542b10\u4e2a\u968f\u673a\u6574\u6570\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u6574\u6570\u57281\u5230100\u4e4b\u95f4\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u751f\u6210\u968f\u673a\u6570\u5b57\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528random\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684<code>random<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>random.random()<\/code>: \u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/li>\n<li><code>random.uniform(a, b)<\/code>: \u751f\u6210\u4e00\u4e2aa\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/li>\n<li><code>random.randint(a, b)<\/code>: \u751f\u6210\u4e00\u4e2aa\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/li>\n<li><code>random.randrange(start, stop[, step])<\/code>: \u4ece\u6307\u5b9a\u8303\u56f4\u5185\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6574\u6570\u3002<\/li>\n<li><code>random.choice(sequence)<\/code>: \u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<li><code>random.sample(population, k)<\/code>: \u4ece\u6307\u5b9a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9k\u4e2a\u5143\u7d20\u3002<\/li>\n<li><code>random.shuffle(sequence)<\/code>: \u5c06\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u968f\u673a\u6253\u4e71\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u751f\u6210\u968f\u673a\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>random.randint(a, b)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5728\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u751f\u62105\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_integers = [random.randint(1, 10) for _ in range(5)]<\/p>\n<p>print(random_integers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53e6\u5916\uff0c\u4f7f\u7528<code>random.randrange(start, stop[, step])<\/code>\u4e5f\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u5b83\u8fd8\u652f\u6301\u6b65\u957f\u53c2\u6570<code>step<\/code>\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_integers = [random.randrange(1, 10, 2) for _ in range(5)]<\/p>\n<p>print(random_integers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u5757\u4f1a\u751f\u62105\u4e2a\u968f\u673a\u6574\u6570\uff0c\u6bcf\u4e2a\u6574\u6570\u57281\u52309\u4e4b\u95f4\uff0c\u5e76\u4e14\u662f\u5947\u6570\uff08\u56e0\u4e3a\u6b65\u957f\u4e3a2\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>random.random()<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u751f\u62105\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_floats = [random.random() for _ in range(5)]<\/p>\n<p>print(random_floats)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_floats = [random.uniform(1.5, 3.5) for _ in range(5)]<\/p>\n<p>print(random_floats)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u751f\u6210\u968f\u673a\u5e8f\u5217<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5e8f\u5217\uff0c\u4f8b\u5982\u4e00\u4e2a\u6253\u4e71\u7684\u5217\u8868\u3002<code>random.shuffle<\/code>\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u968f\u673a\u6253\u4e71\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>sequence = list(range(10))<\/p>\n<p>random.shuffle(sequence)<\/p>\n<p>print(sequence)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.choice<\/code>\u6216<code>random.sample<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>elements = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]<\/p>\n<p>random_element = random.choice(elements)<\/p>\n<p>print(random_element)<\/p>\n<p>random_elements = random.sample(elements, 3)<\/p>\n<p>print(random_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>random.choice<\/code>\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\uff0c\u800c<code>random.sample<\/code>\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u751f\u6210\u968f\u673a\u6570\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u751f\u6210\u968f\u673a\u6570<\/strong>\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6a21\u62df\u548c\u5efa\u6a21<\/strong>\uff1a\u968f\u673a\u6570\u53ef\u4ee5\u7528\u4e8e\u6a21\u62df\u548c\u5efa\u6a21\uff0c\u4f8b\u5982\u8499\u7279\u5361\u7f57\u6a21\u62df\u3002<\/li>\n<li><strong>\u6e38\u620f\u5f00\u53d1<\/strong>\uff1a\u5728\u6e38\u620f\u4e2d\u751f\u6210\u968f\u673a\u4e8b\u4ef6\u3001\u968f\u673a\u5730\u56fe\u3001\u968f\u673a\u654c\u4eba\u7b49\u3002<\/li>\n<li><strong>\u52a0\u5bc6\u548c\u5b89\u5168<\/strong>\uff1a\u5728\u52a0\u5bc6\u7b97\u6cd5\u4e2d\u751f\u6210\u968f\u673a\u5bc6\u94a5\u3002<\/li>\n<li><strong>\u6570\u636e\u5904\u7406<\/strong>\uff1a\u5728\u6570\u636e\u5904\u7406\u4e2d\u751f\u6210\u968f\u673a\u6837\u672c\u3001\u8fdb\u884c\u968f\u673a\u62bd\u6837\u7b49\u3002<\/li>\n<li><strong>\u6d4b\u8bd5<\/strong>\uff1a\u5728\u6d4b\u8bd5\u4e2d\u751f\u6210\u968f\u673a\u6570\u636e\uff0c\u6d4b\u8bd5\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u4f7f\u7528NumPy\u751f\u6210\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Python\u7684<code>random<\/code>\u6a21\u5757\uff0cNumPy\u5e93\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\u3002NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u5b83\u7684<code>numpy.random<\/code>\u6a21\u5757\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5NumPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u751f\u6210\u968f\u673a\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>random_integers = np.random.randint(1, 100, size=10)<\/p>\n<p>print(random_integers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>random_floats = np.random.uniform(1.5, 3.5, size=10)<\/p>\n<p>print(random_floats)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u751f\u6210\u968f\u673a\u5e8f\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>sequence = np.arange(10)<\/p>\n<p>np.random.shuffle(sequence)<\/p>\n<p>print(sequence)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6570\u975e\u5e38\u7b80\u5355\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u6216NumPy\u5e93<\/strong>\u3002<code>random<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u5982\u751f\u6210\u968f\u673a\u6574\u6570\u3001\u968f\u673a\u6d6e\u70b9\u6570\u3001\u968f\u673a\u5e8f\u5217\u7b49\u3002NumPy\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u968f\u673a\u6570\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\u7684\u8303\u56f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u751f\u6210\u968f\u673a\u6570\u3002\u8981\u5b9a\u4e49\u751f\u6210\u968f\u673a\u6570\u7684\u8303\u56f4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.randint(a, b)<\/code>\u65b9\u6cd5\uff0c\u5176\u4e2d<code>a<\/code>\u662f\u8303\u56f4\u7684\u4e0b\u9650\uff0c<code>b<\/code>\u662f\u4e0a\u9650\u3002\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5728<code>a<\/code>\u548c<code>b<\/code>\u4e4b\u95f4\uff08\u5305\u62ec<code>a<\/code>\u548c<code>b<\/code>\uff09\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u751f\u6210\u4e00\u7ec4\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\u5b57\uff1f<\/strong><br \/>\u8981\u751f\u6210\u4e00\u7ec4\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.sample()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u4ece1\u5230100\u4e4b\u95f4\u751f\u621010\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.sample(range(1, 101), 10)<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u6570\u5b57\u4e0d\u91cd\u590d\uff0c\u5e76\u4e14\u5728\u6307\u5b9a\u8303\u56f4\u5185\u3002<\/p>\n<p><strong>\u5982\u4f55\u751f\u6210\u968f\u673a\u5c0f\u6570\u800c\u975e\u6574\u6570\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u751f\u6210\u968f\u673a\u7684\u5c0f\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\u65b9\u6cd5\uff0c\u8fd9\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5728<code>a<\/code>\u548c<code>b<\/code>\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u5408\u9700\u8981\u66f4\u7ec6\u81f4\u968f\u673a\u5316\u7684\u573a\u666f\uff0c\u4f8b\u5982\u751f\u6210\u968f\u673a\u4ef7\u683c\u6216\u5176\u4ed6\u9700\u8981\u5c0f\u6570\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u968f\u673a\u4ea7\u751f\u4e00\u5806\u6570\u5b57\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684random\u6a21\u5757\uff0c\u901a\u8fc7\u4f7f\u7528random\u6a21\u5757\u3001\u751f\u6210\u968f\u673a\u6574\u6570\u3001\u751f\u6210 [&hellip;]","protected":false},"author":3,"featured_media":1093972,"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\/1093955"}],"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=1093955"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093955\/revisions"}],"predecessor-version":[{"id":1093976,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093955\/revisions\/1093976"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1093972"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1093955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1093955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1093955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}