{"id":956459,"date":"2024-12-27T02:35:00","date_gmt":"2024-12-26T18:35:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/956459.html"},"modified":"2024-12-27T02:35:03","modified_gmt":"2024-12-26T18:35:03","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e4%ba%a7%e7%94%9f%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/956459.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u4ea7\u751f\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100104\/ac9c57f5-af71-4ee7-b31a-dfff0fdd7849.webp\" alt=\"python\u5982\u4f55\u968f\u673a\u4ea7\u751f\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u968f\u673a\u4ea7\u751f\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u3001numpy\u5e93\u548csecrets\u5e93\u7b49\u3002\u5177\u4f53\u800c\u8a00\uff0c\u53ef\u4ee5\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u57fa\u672c\u7684\u968f\u673a\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3001\u4f7f\u7528numpy\u5e93\u751f\u6210\u591a\u7ef4\u6570\u7ec4\u7684\u968f\u673a\u6570\u3001\u4f7f\u7528secrets\u5e93\u751f\u6210\u5b89\u5168\u7684\u968f\u673a\u6570\u3002<\/strong> \u5176\u4e2d\uff0crandom\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6a21\u5757\uff0c\u4f7f\u7528\u6700\u4e3a\u5e7f\u6cdb\uff1bnumpy\u5e93\u5219\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u548c\u79d1\u5b66\u8ba1\u7b97\uff1b\u800csecrets\u5e93\u5219\u7528\u4e8e\u751f\u6210\u5bc6\u7801\u5b66\u5b89\u5168\u7684\u968f\u673a\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001RANDOM\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684random\u6a21\u5757\u662f\u751f\u6210\u968f\u673a\u6570\u7684\u4e00\u4e2a\u91cd\u8981\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u751f\u6210\u968f\u673a\u6574\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>random.randint(a, b)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5728a\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u62eca\u548cb\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u4e8e\u9700\u8981\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u6574\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_number = random.randint(1, 10)<\/p>\n<p>print(random_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>random.random()<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u57280.0\u52301.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u82e5\u9700\u8981\u751f\u6210\u5176\u4ed6\u8303\u56f4\u7684\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ebf\u6027\u53d8\u6362\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_float = random.random()<\/p>\n<p>print(random_float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u82e5\u9700\u8981\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_uniform = random.uniform(1.5, 10.5)<\/p>\n<p>print(random_uniform)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u968f\u673a\u5e8f\u5217<\/strong><\/p>\n<\/p>\n<p><p>random\u6a21\u5757\u8fd8\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u968f\u673a\u5e8f\u5217\u3002\u4f7f\u7528<code>random.choice(sequence)<\/code>\u53ef\u4ee5\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\uff0c<code>random.shuffle(sequence)<\/code>\u53ef\u4ee5\u968f\u673a\u6253\u4e71\u5e8f\u5217\u987a\u5e8f\uff0c\u800c<code>random.sample(sequence, k)<\/code>\u53ef\u4ee5\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9k\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">items = [1, 2, 3, 4, 5]<\/p>\n<p>random_choice = random.choice(items)<\/p>\n<p>random.shuffle(items)<\/p>\n<p>random_sample = random.sample(items, 3)<\/p>\n<p>print(random_choice)<\/p>\n<p>print(items)<\/p>\n<p>print(random_sample)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001NUMPY\u5e93<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\u6216\u8fdb\u884c\u590d\u6742\u8ba1\u7b97\u7684\u5e94\u7528\u573a\u666f\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u751f\u6210\u968f\u673a\u6570\u7ec4<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.random.rand(d0, d1, ..., dn)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u5f62\u72b6\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u5728[0, 1)\u8303\u56f4\u5185\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>random_array = np.random.rand(3, 3)<\/p>\n<p>print(random_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u670d\u4ece\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>numpy.random.randn(d0, d1, ..., dn)<\/code>\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u5f62\u72b6\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u670d\u4ece\u6807\u51c6\u6b63\u6001\u5206\u5e03\uff08\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a1\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_normal = np.random.randn(3, 3)<\/p>\n<p>print(random_normal)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u968f\u673a\u6574\u6570\u6570\u7ec4<\/strong><\/p>\n<\/p>\n<p><p><code>numpy.random.randint(low, high=None, size=None, dtype=int)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u548c\u5f62\u72b6\u7684\u968f\u673a\u6574\u6570\u6570\u7ec4\u3002\u82e5\u53ea\u63d0\u4f9blow\u53c2\u6570\uff0c\u5219\u751f\u6210\u4ece0\u5230low\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_int_array = np.random.randint(1, 10, (3, 3))<\/p>\n<p>print(random_int_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001SECRETS\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u9700\u8981\u751f\u6210\u5b89\u5168\u7684\u968f\u673a\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528secrets\u5e93\u3002\u5b83\u751f\u6210\u7684\u968f\u673a\u6570\u9002\u5408\u7528\u4e8e\u5bc6\u7801\u5b66\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u751f\u6210\u5b89\u5168\u7684\u968f\u673a\u6574\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>secrets.randbelow(n)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u57280\u5230n-1\u4e4b\u95f4\u7684\u5b89\u5168\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>secure_random_int = secrets.randbelow(10)<\/p>\n<p>print(secure_random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u5b89\u5168\u7684\u968f\u673a\u5b57\u8282<\/strong><\/p>\n<\/p>\n<p><p><code>secrets.token_bytes(nbytes)<\/code>\u751f\u6210\u4e00\u4e2a\u5305\u542bnbytes\u5b57\u8282\u7684\u5b89\u5168\u968f\u673a\u5b57\u8282\u4e32\u3002<code>secrets.token_hex(nbytes)<\/code>\u8fd4\u56de\u4e00\u4e2a\u5305\u542bnbytes\u5b57\u8282\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">secure_random_bytes = secrets.token_bytes(16)<\/p>\n<p>secure_random_hex = secrets.token_hex(16)<\/p>\n<p>print(secure_random_bytes)<\/p>\n<p>print(secure_random_hex)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u5b89\u5168\u7684URL\u5b89\u5168\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<p><p><code>secrets.token_url<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>(nbytes)<\/code>\u751f\u6210\u4e00\u4e2a\u9002\u5408\u7528\u4f5cURL\u7684\u5b89\u5168\u968f\u673a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">secure_url_token = secrets.token_urlsafe(16)<\/p>\n<p>print(secure_url_token)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5728\u6a21\u62df\u548c\u6e38\u620f\u5f00\u53d1\u4e2d\uff0crandom\u6a21\u5757\u7684\u529f\u80fd\u8db3\u4ee5\u5e94\u5bf9\u5927\u591a\u6570\u9700\u6c42\uff1b\u5728\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0cnumpy\u5e93\u7684\u968f\u673a\u6570\u751f\u6210\u6548\u7387\u66f4\u9ad8\uff1b\u5728\u6d89\u53ca\u5bc6\u7801\u5b66\u7684\u5e94\u7528\u4e2d\uff0csecrets\u5e93\u63d0\u4f9b\u7684\u5b89\u5168\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\u66f4\u4e3a\u9002\u5408\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u968f\u673a\u6570\u7684\u53ef\u91cd\u590d\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u8bc1\u5b9e\u9a8c\u7684\u53ef\u91cd\u590d\u6027\uff0c\u6709\u65f6\u9700\u8981\u8bbe\u7f6e\u968f\u673a\u6570\u751f\u6210\u5668\u7684\u79cd\u5b50\u3002\u53ef\u4ee5\u4f7f\u7528<code>random.seed(a=None)<\/code>\u6216<code>numpy.random.seed(seed=None)<\/code>\u6765\u8bbe\u7f6e\u79cd\u5b50\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random.seed(42)<\/p>\n<p>np.random.seed(42)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u5168\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u7684\u968f\u673a\u6570\u4e0d\u9002\u5408\u7528\u4e8e\u5b89\u5168\u6027\u8981\u6c42\u9ad8\u7684\u573a\u5408\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u4f2a\u968f\u673a\u6570\uff0c\u751f\u6210\u6a21\u5f0f\u53ef\u4ee5\u88ab\u9884\u6d4b\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u573a\u5408\uff0c\u5e94\u4f7f\u7528secrets\u5e93\u751f\u6210\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86Python\u4e2d\u751f\u6210\u968f\u673a\u6570\u7684\u4e0d\u540c\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u5408\uff0c\u9009\u62e9\u5408\u9002\u7684\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u6ee1\u8db3\u5b9e\u9645\u5f00\u53d1\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u751f\u6210\u968f\u673a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u751f\u6210\u968f\u673a\u6570\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528<code>random.randint(a, b)<\/code>\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5728a\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002\u82e5\u60f3\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5728a\u548cb\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u968f\u673a\u6570\u751f\u6210\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u968f\u673a\u6570\u5728\u8bb8\u591a\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u968f\u673a\u62bd\u6837\u53ef\u4ee5\u5e2e\u52a9\u4ece\u5927\u6570\u636e\u96c6\u4e2d\u63d0\u53d6\u6837\u672c\u3002\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c\u968f\u673a\u6570\u7528\u4e8e\u751f\u6210\u4e0d\u53ef\u9884\u6d4b\u7684\u6e38\u620f\u5143\u7d20\u3002\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u968f\u673a\u6570\u7528\u4e8e\u6570\u636e\u96c6\u7684\u5212\u5206\u548c\u6a21\u578b\u7684\u521d\u59cb\u5316\u3002<\/p>\n<p><strong>\u5982\u4f55\u63a7\u5236\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u662f\u53ef\u91cd\u590d\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.seed()<\/code>\u51fd\u6570\u3002\u901a\u8fc7\u8bbe\u7f6e\u79cd\u5b50\u503c\uff0c\u540e\u7eed\u751f\u6210\u7684\u968f\u673a\u6570\u5c06\u662f\u76f8\u540c\u7684\uff0c\u8fd9\u5728\u8c03\u8bd5\u548c\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u8c03\u7528<code>random.seed(42)<\/code>\u540e\uff0c\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u90fd\u4f1a\u5f97\u5230\u76f8\u540c\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u968f\u673a\u4ea7\u751f\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u3001numpy\u5e93\u548csecrets\u5e93\u7b49\u3002\u5177\u4f53\u800c\u8a00\uff0c\u53ef\u4ee5\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":956463,"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\/956459"}],"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=956459"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/956459\/revisions"}],"predecessor-version":[{"id":956464,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/956459\/revisions\/956464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/956463"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=956459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=956459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=956459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}