{"id":1176984,"date":"2025-01-15T17:51:50","date_gmt":"2025-01-15T09:51:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176984.html"},"modified":"2025-01-15T17:51:50","modified_gmt":"2025-01-15T09:51:50","slug":"python-%e5%a6%82%e4%bd%95%e5%8f%96%e9%9a%8f%e6%9c%ba%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176984.html","title":{"rendered":"python \u5982\u4f55\u53d6\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\/25112017\/3dc4220b-341f-43de-9a05-0806bab7d2b8.webp\" alt=\"python \u5982\u4f55\u53d6\u968f\u673a\u6570\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53d6\u968f\u673a\u6570\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528random\u6a21\u5757\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528secrets\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c<strong>random\u6a21\u5757<\/strong>\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u57fa\u7840\u529f\u80fd\uff1b<strong>numpy\u5e93<\/strong>\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u6548\u7684\u968f\u673a\u6570\u751f\u6210\u5668\uff0c\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\uff1b<strong>secrets\u6a21\u5757<\/strong>\u5219\u63d0\u4f9b\u4e86\u66f4\u5b89\u5168\u7684\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd<strong>random\u6a21\u5757<\/strong>\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>random\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u5982<code>random.random()<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u6d6e\u70b9\u6570\uff0c<code>random.randint(a, b)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2aa\u5230b\u4e4b\u95f4\u7684\u6574\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7528\u6cd5\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float = random.random()<\/p>\n<p>print(&quot;Random float between 0 and 1:&quot;, random_float)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_int = random.randint(1, 10)<\/p>\n<p>print(&quot;Random integer between 1 and 10:&quot;, random_int)<\/p>\n<h2><strong>\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>random_choice = random.choice([&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;])<\/p>\n<p>print(&quot;Random choice from list:&quot;, random_choice)<\/p>\n<h2><strong>\u6253\u4e71\u4e00\u4e2a\u5217\u8868\u7684\u987a\u5e8f<\/strong><\/h2>\n<p>random_list = [1, 2, 3, 4, 5]<\/p>\n<p>random.shuffle(random_list)<\/p>\n<p>print(&quot;Shuffled list:&quot;, random_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001RANDOM\u6a21\u5757\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><strong>random\u6a21\u5757<\/strong>\u662fPython\u5185\u7f6e\u7684\u6a21\u5757\uff0c\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\uff0c\u6a21\u62df\u968f\u673a\u4e8b\u4ef6\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u751f\u6210\u4e0d\u540c\u7c7b\u578b\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\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\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u9700\u8981\u4efb\u4f55\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float = random.random()<\/p>\n<p>print(&quot;Random float between 0 and 1:&quot;, random_float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c<code>random.uniform(a, b)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2aa\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a1.5\u52305.5\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/p>\n<p>random_uniform = random.uniform(1.5, 5.5)<\/p>\n<p>print(&quot;Random float between 1.5 and 5.5:&quot;, random_uniform)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>random.randint(a, b)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2aa\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u62eca\u548cb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/p>\n<p>random_int = random.randint(1, 10)<\/p>\n<p>print(&quot;Random integer between 1 and 10:&quot;, random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u751f\u6210\u4e0d\u5305\u62ec\u4e0a\u9650\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.randrange(start, stop[, step])<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u4e0d\u5305\u62ec10<\/p>\n<p>random_randrange = random.randrange(1, 10)<\/p>\n<p>print(&quot;Random integer between 1 and 10 (excluding 10):&quot;, random_randrange)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>random.choice(seq)<\/code>\u53ef\u4ee5\u4ece\u4e00\u4e2a\u975e\u7a7a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002\u5e8f\u5217\u53ef\u4ee5\u662f\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>random_choice = random.choice([&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;])<\/p>\n<p>print(&quot;Random choice from list:&quot;, random_choice)<\/p>\n<h2><strong>\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5b57\u7b26<\/strong><\/h2>\n<p>random_char = random.choice(&#39;abcdefg&#39;)<\/p>\n<p>print(&quot;Random character from string:&quot;, random_char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u6253\u4e71\u5e8f\u5217<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>random.shuffle(seq)<\/code>\u53ef\u4ee5\u6253\u4e71\u4e00\u4e2a\u5e8f\u5217\u7684\u987a\u5e8f\u3002\u8fd9\u4e2a\u65b9\u6cd5\u76f4\u63a5\u4fee\u6539\u539f\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u4e71\u4e00\u4e2a\u5217\u8868\u7684\u987a\u5e8f<\/p>\n<p>random_list = [1, 2, 3, 4, 5]<\/p>\n<p>random.shuffle(random_list)<\/p>\n<p>print(&quot;Shuffled list:&quot;, random_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001NUMPY\u5e93\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><strong>numpy\u5e93<\/strong>\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u968f\u673a\u6570\u751f\u6210\u5668\uff0c\u7279\u522b\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u5927\u6570\u636e\u5904\u7406\u3002numpy\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u4f4d\u4e8e<code>numpy.random<\/code>\u6a21\u5757\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.random.rand(d0, d1, ..., dn)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u4e3a(d0, d1, &#8230;, dn)\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a3x3\u7684\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4<\/strong><\/h2>\n<p>random_array = np.random.rand(3, 3)<\/p>\n<p>print(&quot;Random 3x3 array:&quot;, random_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u751f\u6210\u5176\u4ed6\u8303\u56f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random.uniform(low, high, size)<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a1.5\u52305.5\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4<\/p>\n<p>random_uniform_array = np.random.uniform(1.5, 5.5, size=(3, 3))<\/p>\n<p>print(&quot;Random 3x3 array with values between 1.5 and 5.5:&quot;, random_uniform_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.random.randint(low, high=None, size=None, dtype=&#39;l&#39;)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6574\u6570\u6216\u968f\u673a\u6574\u6570\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u6570\u7ec4<\/p>\n<p>random_int_array = np.random.randint(1, 10, size=(3, 3))<\/p>\n<p>print(&quot;Random 3x3 integer array between 1 and 10:&quot;, random_int_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4ece\u6570\u7ec4\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.random.choice(a, size=None, replace=True, p=None)<\/code>\u53ef\u4ee5\u4ece\u4e00\u4e2a\u6570\u7ec4\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u4e00\u4e2a\u6570\u7ec4\u4e2d\u968f\u673a\u9009\u62e93\u4e2a\u5143\u7d20<\/p>\n<p>random_choice_array = np.random.choice([1, 2, 3, 4, 5], size=3)<\/p>\n<p>print(&quot;Random choice array:&quot;, random_choice_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u751f\u6210\u7b26\u5408\u7279\u5b9a\u5206\u5e03\u7684\u968f\u673a\u6570<\/h4>\n<\/p>\n<p><p>numpy\u8fd8\u63d0\u4f9b\u4e86\u751f\u6210\u7b26\u5408\u7279\u5b9a\u5206\u5e03\u7684\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u5982\u6b63\u6001\u5206\u5e03\u3001\u6cca\u677e\u5206\u5e03\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a1\u76843x3\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\u7ec4<\/p>\n<p>random_normal_array = np.random.normal(0, 1, size=(3, 3))<\/p>\n<p>print(&quot;Random 3x3 normal distribution array:&quot;, random_normal_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001SECRETS\u6a21\u5757\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><strong>secrets\u6a21\u5757<\/strong>\u662fPython 3.6\u5f15\u5165\u7684\uff0c\u7528\u4e8e\u751f\u6210\u5b89\u5168\u7684\u968f\u673a\u6570\uff0c\u9002\u7528\u4e8e\u5bc6\u7801\u5b66\u76f8\u5173\u7684\u5e94\u7528\u573a\u666f\u3002\u76f8\u6bd4random\u6a21\u5757\uff0csecrets\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u968f\u673a\u6027\u548c\u4e0d\u53ef\u9884\u6d4b\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>secrets.randbelow(n)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a0\u5230n-1\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a0\u52309\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_secure_int = secrets.randbelow(10)<\/p>\n<p>print(&quot;Secure random integer between 0 and 9:&quot;, random_secure_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u968f\u673a\u5b57\u8282<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>secrets.token_bytes(nbytes)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u957f\u5ea6\u4e3anbytes\u7684\u968f\u673a\u5b57\u8282\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a\u957f\u5ea6\u4e3a16\u7684\u968f\u673a\u5b57\u8282\u4e32<\/p>\n<p>random_bytes = secrets.token_bytes(16)<\/p>\n<p>print(&quot;Random bytes:&quot;, random_bytes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u751f\u6210\u968f\u673aURL\u5b89\u5168\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>secrets.token_url<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>(nbytes=None)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684URL\u5b89\u5168\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a\u957f\u5ea6\u4e3a16\u7684\u968f\u673aURL\u5b89\u5168\u5b57\u7b26\u4e32<\/p>\n<p>random_urlsafe = secrets.token_urlsafe(16)<\/p>\n<p>print(&quot;Random URL safe string:&quot;, random_urlsafe)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u53ca\u793a\u4f8b<\/h3>\n<\/p>\n<p><h4>1\u3001\u6a21\u62df\u63b7\u9ab0\u5b50<\/h4>\n<\/p>\n<p><p>\u63b7\u9ab0\u5b50\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u968f\u673a\u6570\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528random\u6a21\u5757\u6765\u6a21\u62df\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u6a21\u62df\u63b7\u4e00\u4e2a\u9ab0\u5b50\uff0c\u8fd4\u56de1\u52306\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>def roll_dice():<\/p>\n<p>    return random.randint(1, 6)<\/p>\n<h2><strong>\u6a21\u62df\u63b7\u4e24\u4e2a\u9ab0\u5b50\uff0c\u8fd4\u56de\u4e24\u4e2a1\u52306\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>def roll_two_dice():<\/p>\n<p>    return (random.randint(1, 6), random.randint(1, 6))<\/p>\n<p>print(&quot;Roll one dice:&quot;, roll_dice())<\/p>\n<p>print(&quot;Roll two dice:&quot;, roll_two_dice())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u968f\u673a\u5bc6\u7801<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5bc6\u7801\u53ef\u4ee5\u4f7f\u7528secrets\u6a21\u5757\uff0c\u4ee5\u786e\u4fdd\u5bc6\u7801\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>import string<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5bc6\u7801\uff0c\u5305\u542b\u5927\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u7b26\u53f7\uff0c\u957f\u5ea6\u4e3a12<\/strong><\/h2>\n<p>def generate_password(length=12):<\/p>\n<p>    characters = string.ascii_letters + string.digits + string.punctuation<\/p>\n<p>    password = &#39;&#39;.join(secrets.choice(characters) for i in range(length))<\/p>\n<p>    return password<\/p>\n<p>print(&quot;Random password:&quot;, generate_password())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u968f\u673a\u62bd\u6837<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u968f\u673a\u62bd\u6837\u662f\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4ece\u4e00\u4e2a\u6570\u7ec4\u4e2d\u968f\u673a\u62bd\u6837\uff0c\u8fd4\u56de\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20<\/strong><\/h2>\n<p>def random_sampling(data, sample_size):<\/p>\n<p>    return np.random.choice(data, size=sample_size, replace=False)<\/p>\n<p>data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])<\/p>\n<p>sample_size = 3<\/p>\n<p>print(&quot;Random sampling:&quot;, random_sampling(data, sample_size))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u968f\u673a\u6570\u79cd\u5b50<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u751f\u6210\u53ef\u91cd\u590d\u7684\u968f\u673a\u6570\u5e8f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u6570\u79cd\u5b50\u6765\u5b9e\u73b0\u3002random\u6a21\u5757\u548cnumpy\u5e93\u90fd\u652f\u6301\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528\u76f8\u540c\u7684\u79cd\u5b50\uff0c\u53ef\u4ee5\u751f\u6210\u76f8\u540c\u7684\u968f\u673a\u6570\u5e8f\u5217<\/strong><\/h2>\n<p>random.seed(42)<\/p>\n<p>print(&quot;Random float with seed 42:&quot;, random.random())<\/p>\n<p>np.random.seed(42)<\/p>\n<p>print(&quot;Random array with seed 42:&quot;, np.random.rand(3, 3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u968f\u673a\u6570\u7684\u5b89\u5168\u6027<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u5982\u5bc6\u7801\u751f\u6210\u3001\u5bc6\u94a5\u751f\u6210\u7b49\uff0c\u5efa\u8bae\u4f7f\u7528secrets\u6a21\u5757\uff0c\u800c\u4e0d\u662frandom\u6a21\u5757\u6216numpy\u5e93\u3002secrets\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u968f\u673a\u6027\u548c\u4e0d\u53ef\u9884\u6d4b\u6027\uff0c\u9002\u5408\u5bc6\u7801\u5b66\u76f8\u5173\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5b89\u5168\u7684\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_secure_int = secrets.randbelow(10)<\/p>\n<p>print(&quot;Secure random integer between 0 and 9:&quot;, random_secure_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u968f\u673a\u6570\u7684\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u548c\u79d1\u5b66\u8ba1\u7b97\uff0c\u5efa\u8bae\u4f7f\u7528numpy\u5e93\u3002numpy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u968f\u673a\u6570\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u6027\u80fd\u4f18\u4e8erandom\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a1000x1000\u7684\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4<\/strong><\/h2>\n<p>random_large_array = np.random.rand(1000, 1000)<\/p>\n<p>print(&quot;Random 1000x1000 array generated using numpy&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<strong>random\u6a21\u5757<\/strong>\u9002\u5408\u57fa\u7840\u7684\u968f\u673a\u6570\u751f\u6210\uff0c<strong>numpy\u5e93<\/strong>\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0c<strong>secrets\u6a21\u5757<\/strong>\u9002\u5408\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u548c\u5b89\u5168\u6027\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5404\u79cd\u968f\u673a\u6570\u751f\u6210\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u53d6\u968f\u673a\u6570\u7684\u4e3b\u8981\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u5177\u4f53\u793a\u4f8b\u5c55\u793a\u4e86\u4e0d\u540c\u65b9\u6cd5\u7684\u5e94\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\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\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u6765\u751f\u6210\u968f\u673a\u6574\u6570\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.randint(a, b)<\/code>\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5728<code>a<\/code>\u548c<code>b<\/code>\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u62ec<code>a<\/code>\u548c<code>b<\/code>\u3002\u4f8b\u5982\uff0c<code>random.randint(1, 10)<\/code>\u4f1a\u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u8981\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5728<code>a<\/code>\u548c<code>b<\/code>\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u5305\u542b<code>a<\/code>\u4f46\u4e0d\u4e00\u5b9a\u5305\u542b<code>b<\/code>\u3002\u4f8b\u5982\uff0c<code>random.uniform(1.0, 5.0)<\/code>\u4f1a\u57281.0\u548c5.0\u4e4b\u95f4\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ecePython\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u60f3\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.choice()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u9009\u62e9\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>random.choice([&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;])<\/code>\u4f1a\u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6c34\u679c\u540d\u79f0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53d6\u968f\u673a\u6570\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528random\u6a21\u5757\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528secrets\u6a21\u5757\u3002 \u5176\u4e2d\uff0cr [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176984"}],"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=1176984"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176984\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1176984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1176984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1176984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}