{"id":1030640,"date":"2024-12-31T11:18:44","date_gmt":"2024-12-31T03:18:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1030640.html"},"modified":"2024-12-31T11:18:46","modified_gmt":"2024-12-31T03:18:46","slug":"python3%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e9%9a%8f%e6%9c%ba%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1030640.html","title":{"rendered":"python3\u5982\u4f55\u521b\u5efa\u968f\u673a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/975181b7-cbd2-4cde-a2ff-c43011712e91.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python3\u5982\u4f55\u521b\u5efa\u968f\u673a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python 3\u4e2d\u521b\u5efa\u968f\u673a\u6570\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5<\/strong>\uff0c<strong>\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684random\u6a21\u5757\u3001numpy\u5e93\u3001secrets\u6a21\u5757\u7b49\u3002<\/strong>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u89e3\u91ca\u5982\u4f55\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684random\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u6a21\u5757<code>random<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1.1 \u751f\u6210\u4e00\u4e2a\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>random()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_float = random.random()<\/p>\n<p>print(f&quot;\u968f\u673a\u751f\u6210\u7684\u6d6e\u70b9\u6570: {random_float}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>randint(a, b)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5728a\u548cb\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff08\u5305\u62eca\u548cb\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_int = random.randint(1, 10)<\/p>\n<p>print(f&quot;\u968f\u673a\u751f\u6210\u7684\u6574\u6570: {random_int}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3 \u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>choice(seq)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4ece\u4e00\u4e2a\u975e\u7a7a\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\uff09\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>random_element = random.choice(elements)<\/p>\n<p>print(f&quot;\u968f\u673a\u9009\u62e9\u7684\u5143\u7d20: {random_element}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.4 \u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>uniform(a, b)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5728a\u548cb\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_uniform = random.uniform(1.5, 10.5)<\/p>\n<p>print(f&quot;\u968f\u673a\u751f\u6210\u7684\u6d6e\u70b9\u6570: {random_uniform}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5b83\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5<code>numpy<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.1 \u751f\u6210\u4e00\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.random.rand()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6307\u5b9a\u7ef4\u5ea6\u7684\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4\uff0c\u6240\u6709\u503c\u57280\u52301\u4e4b\u95f4\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(f&quot;\u968f\u673a\u751f\u6210\u7684\u6d6e\u70b9\u6570\u6570\u7ec4: \\n{random_array}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.random.randint(low, high, size)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6307\u5b9a\u7ef4\u5ea6\u7684\u968f\u673a\u6574\u6570\u6570\u7ec4\uff0c\u6240\u6709\u503c\u5728low\u548chigh\u4e4b\u95f4\uff08\u4e0d\u5305\u62echigh\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_int_array = np.random.randint(1, 10, size=(3, 3))<\/p>\n<p>print(f&quot;\u968f\u673a\u751f\u6210\u7684\u6574\u6570\u6570\u7ec4: \\n{random_int_array}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528secrets\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u751f\u6210\u5bc6\u7801\u6216\u5176\u4ed6\u9700\u8981\u9ad8\u5b89\u5168\u6027\u968f\u673a\u6570\u7684\u573a\u666f\uff0c<code>secrets<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002\u5b83\u7528\u4e8e\u751f\u6210\u52a0\u5bc6\u5f3a\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u751f\u6210\u4e00\u4e2a\u5b89\u5168\u7684\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>secrets.randbelow(n)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u57280\u548cn-1\u4e4b\u95f4\u7684\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(f&quot;\u5b89\u5168\u7684\u968f\u673a\u6574\u6570: {secure_random_int}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u751f\u6210\u4e00\u4e2a\u5b89\u5168\u7684\u968f\u673a\u5b57\u8282\u4e32<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>secrets.token_bytes(n)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5305\u542bn\u4e2a\u5b57\u8282\u7684\u968f\u673a\u5b57\u8282\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">secure_random_bytes = secrets.token_bytes(16)<\/p>\n<p>print(f&quot;\u5b89\u5168\u7684\u968f\u673a\u5b57\u8282\u4e32: {secure_random_bytes}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u548c\u9009\u62e9\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>4.1 \u6e38\u620f\u5f00\u53d1<\/h4>\n<\/p>\n<p><p>\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c\u968f\u673a\u6570\u5e38\u7528\u4e8e\u751f\u6210\u6e38\u620f\u4e2d\u7684\u968f\u673a\u4e8b\u4ef6\u3001\u968f\u673a\u5730\u56fe\u7b49\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u5df2\u7ecf\u8db3\u591f\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u548c\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u5e38\u9700\u8981\u751f\u6210\u968f\u673a\u6570\u636e\u8fdb\u884c\u6a21\u62df\u6216\u6d4b\u8bd5\u3002<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\uff0c\u9002\u5408\u7528\u4e8e\u8fd9\u4e9b\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>4.3 \u9ad8\u5b89\u5168\u6027\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u5728\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u573a\u666f\u4e2d\uff0c\u5982\u751f\u6210\u5bc6\u7801\u3001\u5bc6\u94a5\u7b49\uff0c\u5efa\u8bae\u4f7f\u7528<code>secrets<\/code>\u6a21\u5757\u6765\u751f\u6210\u52a0\u5bc6\u5f3a\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python 3\u4e2d\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u3002<strong>\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\uff0c<code>numpy<\/code>\u5e93\u9002\u7528\u4e8e\u9700\u8981\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\u6216\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u573a\u666f\uff0c<code>secrets<\/code>\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u573a\u666f\u3002<\/strong>\u4e86\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u7075\u6d3b\u5730\u751f\u6210\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python3\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u7684<code>randint()<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u53ea\u9700\u5bfc\u5165<code>random<\/code>\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528<code>random.randint(a, b)<\/code>\uff0c\u5176\u4e2d<code>a<\/code>\u662f\u8303\u56f4\u7684\u4e0b\u9650\uff0c<code>b<\/code>\u662f\u4e0a\u9650\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import random\nrandom_number = random.randint(1, 10)  # \u751f\u62101\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\nprint(random_number)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u751f\u6210\u6307\u5b9a\u6570\u91cf\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u751f\u6210\u591a\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\u6765\u751f\u6210\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u5e76\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u6240\u9700\u6570\u91cf\u7684\u968f\u673a\u6570\u3002\u4f8b\u5982\uff0c\u751f\u62105\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import random\nrandom_floats = [random.uniform(0, 1) for _ in range(5)]\nprint(random_floats)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u6570\u5177\u6709\u53ef\u91cd\u590d\u6027\uff1f<\/strong><br \/>\u5728\u751f\u6210\u968f\u673a\u6570\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u786e\u4fdd\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u76f8\u540c\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50\u6765\u5b9e\u73b0\uff0c\u4f7f\u7528<code>random.seed(a)<\/code>\u65b9\u6cd5\uff0c\u5176\u4e2d<code>a<\/code>\u53ef\u4ee5\u662f\u4efb\u4f55\u6574\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import random\nrandom.seed(42)  # \u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50\nprint(random.randint(1, 100))  # \u6bcf\u6b21\u8fd0\u884c\u4f1a\u5f97\u5230\u76f8\u540c\u7684\u968f\u673a\u6570\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u8c03\u8bd5\u6216\u91cd\u590d\u5b9e\u9a8c\u65f6\uff0c\u968f\u673a\u6570\u7684\u7ed3\u679c\u4e00\u81f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python 3\u4e2d\u521b\u5efa\u968f\u673a\u6570\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684random\u6a21\u5757\u3001numpy\u5e93\u3001secrets\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1030646,"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\/1030640"}],"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=1030640"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030640\/revisions"}],"predecessor-version":[{"id":1030650,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030640\/revisions\/1030650"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1030646"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1030640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1030640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1030640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}