{"id":1143366,"date":"2025-01-08T22:49:27","date_gmt":"2025-01-08T14:49:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1143366.html"},"modified":"2025-01-08T22:49:30","modified_gmt":"2025-01-08T14:49:30","slug":"python%e5%a6%82%e4%bd%95%e4%b8%80%e6%ac%a1%e6%80%a7%e5%8f%96%e5%87%ba%e5%a4%9a%e4%b8%aa%e9%9a%8f%e6%9c%ba%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1143366.html","title":{"rendered":"python\u5982\u4f55\u4e00\u6b21\u6027\u53d6\u51fa\u591a\u4e2a\u968f\u673a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181021\/9b68de2b-e03e-4b82-8a30-55d6add10f24.webp\" alt=\"python\u5982\u4f55\u4e00\u6b21\u6027\u53d6\u51fa\u591a\u4e2a\u968f\u673a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4e00\u6b21\u6027\u53d6\u51fa\u591a\u4e2a\u968f\u673a\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e09\u79cd\uff1a\u4f7f\u7528random.sample()\u51fd\u6570\u3001\u4f7f\u7528random.choices()\u51fd\u6570\u3001\u5229\u7528numpy\u5e93\u4e2d\u7684numpy.random.choice()\u51fd\u6570<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u53ca\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528random.sample()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>random.sample()\u51fd\u6570<\/strong>\u662fPython\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u4ece\u6307\u5b9a\u7684\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\uff0c\u4e14\u4e0d\u4f1a\u91cd\u590d\u3002\u8fd9\u4e2a\u51fd\u6570\u975e\u5e38\u9002\u7528\u4e8e\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u9009\u62e9\u591a\u4e2a\u72ec\u7279\u7684\u968f\u673a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>population = range(1, 101)<\/p>\n<p>sample_size = 10<\/p>\n<p>random_numbers = random.sample(population, sample_size)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4e0d\u4f1a\u91cd\u590d<\/strong>\uff1arandom.sample()\u51fd\u6570\u786e\u4fdd\u8fd4\u56de\u7684\u5143\u7d20\u662f\u72ec\u7279\u7684\uff0c\u4e0d\u4f1a\u6709\u91cd\u590d\u3002<\/li>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1a\u53ea\u9700\u4f20\u5165\u4e00\u4e2a\u5e8f\u5217\u548c\u6837\u672c\u5927\u5c0f\u5373\u53ef\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9650\u5236\u5728\u5e8f\u5217\u4e2d\u9009\u62e9<\/strong>\uff1a\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u5e8f\u5217\u4f5c\u4e3a\u8f93\u5165\uff0c\u4e0d\u80fd\u76f4\u63a5\u751f\u6210\u8303\u56f4\u5916\u7684\u968f\u673a\u6570\u3002<\/li>\n<li><strong>\u6837\u672c\u5927\u5c0f\u9650\u5236<\/strong>\uff1a\u6837\u672c\u5927\u5c0f\u4e0d\u80fd\u8d85\u8fc7\u5e8f\u5217\u7684\u957f\u5ea6\uff0c\u5426\u5219\u4f1a\u629b\u51faValueError\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528random.choices()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>random.choices()\u51fd\u6570<\/strong>\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u4ece\u6307\u5b9a\u7684\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\uff0c\u4e14\u5141\u8bb8\u91cd\u590d\u3002\u8fd9\u4e2a\u51fd\u6570\u9002\u7528\u4e8e\u9700\u8981\u968f\u673a\u9009\u62e9\u591a\u4e2a\u5143\u7d20\u4e14\u5141\u8bb8\u91cd\u590d\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>population = range(1, 101)<\/p>\n<p>sample_size = 10<\/p>\n<p>random_numbers = random.choices(population, k=sample_size)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u5141\u8bb8\u91cd\u590d<\/strong>\uff1arandom.choices()\u51fd\u6570\u5141\u8bb8\u8fd4\u56de\u7684\u5143\u7d20\u6709\u91cd\u590d\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u6307\u5b9a\u6743\u91cd\uff0c\u4f7f\u5f97\u67d0\u4e9b\u5143\u7d20\u88ab\u9009\u4e2d\u7684\u6982\u7387\u66f4\u9ad8\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u53ef\u80fd\u6709\u91cd\u590d\u5143\u7d20<\/strong>\uff1a\u8fd4\u56de\u7684\u7ed3\u679c\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u7684\u5143\u7d20\uff0c\u8fd9\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u53ef\u80fd\u4e0d\u5408\u9002\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5229\u7528numpy\u5e93\u4e2d\u7684numpy.random.choice()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>numpy.random.choice()\u51fd\u6570<\/strong>\u662fNumPy\u5e93\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u4ece\u4e00\u4e2a\u6570\u7ec4\u4e2d\u968f\u673a\u9009\u62e9\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\uff0c\u4e14\u53ef\u4ee5\u63a7\u5236\u662f\u5426\u5141\u8bb8\u91cd\u590d\u3002NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6027\u80fd\u548c\u5927\u91cf\u968f\u673a\u6570\u751f\u6210\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>population = np.arange(1, 101)<\/p>\n<p>sample_size = 10<\/p>\n<p>random_numbers = np.random.choice(population, sample_size, replace=False)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd<\/strong>\uff1aNumPy\u5e93\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u6027\u80fd\u4f18\u5f02\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u63a7\u5236\u662f\u5426\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9700\u8981\u5b89\u88c5\u989d\u5916\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u8fd9\u5bf9\u4e00\u4e9b\u8f7b\u91cf\u7ea7\u9879\u76ee\u53ef\u80fd\u4e0d\u5408\u9002\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5\u4e0e\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u9ad8\u7ea7\u5e94\u7528\u548c\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u66f4\u9ad8\u6548\u5730\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u6d6e\u70b9\u6570\u968f\u673a\u6570<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u751f\u6210\u6d6e\u70b9\u6570\u800c\u4e0d\u662f\u6574\u6570\u3002\u53ef\u4ee5\u4f7f\u7528random.uniform()\u6216numpy.random.uniform()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>sample_size = 10<\/p>\n<p>random_numbers = [random.uniform(1.0, 100.0) for _ in range(sample_size)]<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>sample_size = 10<\/p>\n<p>random_numbers = np.random.uniform(1.0, 100.0, sample_size)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7edf\u8ba1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u751f\u6210\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u3002\u53ef\u4ee5\u4f7f\u7528numpy.random.normal()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>sample_size = 10<\/p>\n<p>mean = 50<\/p>\n<p>std_dev = 10<\/p>\n<p>random_numbers = np.random.normal(mean, std_dev, sample_size)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u751f\u6210\u7b26\u5408\u7279\u5b9a\u5206\u5e03\u7684\u968f\u673a\u6570<\/h4>\n<\/p>\n<p><p>NumPy\u5e93\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u5206\u5e03\u51fd\u6570\uff0c\u5982numpy.random.binomial()\u3001numpy.random.poisson()\u7b49\uff0c\u53ef\u4ee5\u751f\u6210\u7b26\u5408\u7279\u5b9a\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>sample_size = 10<\/p>\n<p>n, p = 10, 0.5<\/p>\n<p>random_numbers = np.random.binomial(n, p, sample_size)<\/p>\n<p>print(random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u91cf\u56e0\u7d20\u3002\u4e0b\u9762\u662f\u5bf9\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210100\u4e07\u4e2a\u968f\u673a\u6570\u7684\u6027\u80fd\u6bd4\u8f83<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import numpy as np<\/p>\n<p>import time<\/p>\n<h2><strong>random.sample()<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>population = range(1, 1000001)<\/p>\n<p>random_numbers = random.sample(population, 1000000)<\/p>\n<p>print(&quot;random.sample() took %s seconds&quot; % (time.time() - start_time))<\/p>\n<h2><strong>random.choices()<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>random_numbers = random.choices(population, k=1000000)<\/p>\n<p>print(&quot;random.choices() took %s seconds&quot; % (time.time() - start_time))<\/p>\n<h2><strong>numpy.random.choice()<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>population = np.arange(1, 1000001)<\/p>\n<p>random_numbers = np.random.choice(population, 1000000, replace=False)<\/p>\n<p>print(&quot;numpy.random.choice() took %s seconds&quot; % (time.time() - start_time))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7ed3\u679c\u5206\u6790<\/h4>\n<\/p>\n<ul>\n<li><strong>random.sample()<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u6027\u80fd\u8f83\u4f4e\u3002<\/li>\n<li><strong>random.choices()<\/strong>\uff1a\u5141\u8bb8\u91cd\u590d\uff0c\u6027\u80fd\u8f83\u9ad8\u3002<\/li>\n<li><strong>numpy.random.choice()<\/strong>\uff1a\u6027\u80fd\u6700\u4f18\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u62bd\u6837<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u4ece\u6570\u636e\u96c6\u4e2d\u62bd\u6837\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u5feb\u901f\u751f\u6210\u6837\u672c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import pandas as pd<\/p>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>sample_size = 100<\/p>\n<p>sample_data = data.sample(n=sample_size, random_state=42)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6a21\u62df\u5b9e\u9a8c<\/h4>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u7814\u7a76\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6a21\u62df\u5b9e\u9a8c\u3002\u53ef\u4ee5\u4f7f\u7528numpy.random.choice()\u51fd\u6570\u751f\u6210\u6a21\u62df\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>num_simulations = 1000<\/p>\n<p>sample_size = 10<\/p>\n<p>results = []<\/p>\n<p>for _ in range(num_simulations):<\/p>\n<p>    sample = np.random.choice(np.arange(1, 101), sample_size, replace=False)<\/p>\n<p>    results.append(sample.mean())<\/p>\n<p>print(&quot;Mean of simulated samples: &quot;, np.mean(results))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e00\u6b21\u6027\u53d6\u51fa\u591a\u4e2a\u968f\u673a\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u9009\u62e9\uff0c\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>random.sample()\u9002\u7528\u4e8e\u9700\u8981\u72ec\u7279\u968f\u673a\u6570\u7684\u60c5\u51b5<\/strong>\uff0c<strong>random.choices()\u9002\u7528\u4e8e\u5141\u8bb8\u91cd\u590d\u7684\u60c5\u51b5<\/strong>\uff0c\u800c<strong>numpy.random.choice()\u5219\u9002\u5408\u9700\u8981\u9ad8\u6027\u80fd\u548c\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u7684\u573a\u666f<\/strong>\u3002\u6b64\u5916\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5206\u5e03\u51fd\u6570\uff0c\u53ef\u4ee5\u751f\u6210\u7b26\u5408\u5404\u79cd\u5206\u5e03\u7684\u968f\u673a\u6570\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u5e94\u7528\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u6765\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\u3002\u901a\u8fc7<code>random.sample()<\/code>\u51fd\u6570\u53ef\u4ee5\u4ece\u6307\u5b9a\u8303\u56f4\u5185\u9009\u62e9\u591a\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\uff0c\u6216\u8005\u4f7f\u7528<code>random.choices()<\/code>\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\uff0c\u5305\u62ec\u91cd\u590d\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import random\n\n# \u751f\u62105\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\nunique_randoms = random.sample(range(1, 101), 5)\nprint(unique_randoms)\n\n# \u751f\u62105\u4e2a\u53ef\u80fd\u91cd\u590d\u7684\u968f\u673a\u6570\nrandoms_with_replacement = random.choices(range(1, 101), k=5)\nprint(randoms_with_replacement)\n<\/code><\/pre>\n<p><strong>\u751f\u6210\u968f\u673a\u6570\u65f6\u5982\u4f55\u8bbe\u7f6e\u8303\u56f4\u548c\u6570\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a<code>range()<\/code>\u51fd\u6570\u7684\u53c2\u6570\u6765\u8bbe\u7f6e\u968f\u673a\u6570\u7684\u8303\u56f4\u3002\u4f8b\u5982\uff0c<code>range(1, 101)<\/code>\u8868\u793a\u751f\u62101\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6570\u3002\u5982\u679c\u5e0c\u671b\u83b7\u5f97\u7279\u5b9a\u6570\u91cf\u7684\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>k<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u9700\u8981\u7684\u6570\u91cf\uff0c\u4f8b\u5982<code>random.choices(range(1, 101), k=10)<\/code>\u5c06\u751f\u621010\u4e2a\u968f\u673a\u6570\u3002<\/p>\n<p><strong>\u4f7f\u7528numpy\u751f\u6210\u968f\u673a\u6570\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528<code>numpy<\/code>\u5e93\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002<code>numpy.random<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\uff0c\u5982<code>numpy.random.randint()<\/code>\u548c<code>numpy.random.rand()<\/code>\uff0c\u80fd\u591f\u5feb\u901f\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\uff0c\u4e14\u652f\u6301\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u63d0\u5347\u8ba1\u7b97\u6027\u80fd\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u751f\u621010\u4e2a1\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\nrandom_numbers = np.random.randint(1, 101, size=10)\nprint(random_numbers)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u6570\u636e\u79d1\u5b66\u548c\u673a\u5668\u5b66\u4e60\u4e2d\u975e\u5e38\u5e38\u7528\uff0c\u80fd\u591f\u6ee1\u8db3\u9ad8\u6548\u8ba1\u7b97\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4e00\u6b21\u6027\u53d6\u51fa\u591a\u4e2a\u968f\u673a\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e09\u79cd\uff1a\u4f7f\u7528random.sample()\u51fd\u6570\u3001\u4f7f\u7528rando [&hellip;]","protected":false},"author":3,"featured_media":1143373,"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\/1143366"}],"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=1143366"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143366\/revisions"}],"predecessor-version":[{"id":1143376,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143366\/revisions\/1143376"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1143373"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1143366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1143366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1143366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}