{"id":1065437,"date":"2024-12-31T16:17:34","date_gmt":"2024-12-31T08:17:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1065437.html"},"modified":"2024-12-31T16:17:36","modified_gmt":"2024-12-31T08:17:36","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8random%e7%94%9f%e6%88%90%e9%9a%8f%e6%9c%ba%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1065437.html","title":{"rendered":"python\u5982\u4f55\u7528random\u751f\u6210\u968f\u673a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/6754b4c0-598b-4070-821e-662b5cf4418e.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u7528random\u751f\u6210\u968f\u673a\u6570\" \/><\/p>\n<p><p> <strong>Python\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528random\u6a21\u5757\u3001\u751f\u6210\u968f\u673a\u6574\u6570\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u3001\u751f\u6210\u8303\u56f4\u5185\u7684\u968f\u673a\u6570<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528random\u6a21\u5757<\/strong>\u662f\u751f\u6210\u968f\u673a\u6570\u7684\u57fa\u7840\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u4e0d\u540c\u7c7b\u578b\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><p>Python\u7684random\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u751f\u6210\u968f\u673a\u6574\u6570\u3001\u6d6e\u70b9\u6570\u4ee5\u53ca\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u968f\u673a\u6570\u751f\u6210\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528random\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6570\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165random\u6a21\u5757\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u751f\u6210\u968f\u673a\u6570\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5927\u90e8\u5206\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u751f\u6210\u968f\u673a\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u968f\u673a\u6574\u6570\u662f\u6700\u5e38\u89c1\u7684\u9700\u6c42\u4e4b\u4e00\u3002random\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u4e2a\u51fd\u6570\u6765\u751f\u6210\u968f\u673a\u6574\u6570\uff0c\u5305\u62ec<code>randint()<\/code>\u3001<code>randrange()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528randint()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>randint(a, b)<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aa\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff08\u5305\u62eca\u548cb\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_integer = random.randint(1, 10)<\/p>\n<p>print(random_integer)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528randrange()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>randrange(start, stop[, step])<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u4ecestart\u5230stop\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u4e0d\u5305\u62ecstop\u3002\u53ef\u4ee5\u6307\u5b9a\u6b65\u957fstep\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_integer = random.randrange(1, 10, 2)<\/p>\n<p>print(random_integer)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u4e5f\u662f\u5e38\u89c1\u9700\u6c42\u4e4b\u4e00\u3002random\u6a21\u5757\u63d0\u4f9b\u4e86<code>random()<\/code>, <code>uniform()<\/code>, <code>gauss()<\/code>\u7b49\u591a\u79cd\u51fd\u6570\u6765\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528random()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>random()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a0.0\u52301.0\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(random_float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528uniform()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>uniform(a, b)<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aa\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff08\u5305\u62eca\u548cb\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_float = random.uniform(1.0, 10.0)<\/p>\n<p>print(random_float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528gauss()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>gauss(mu, sigma)<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u5176\u4e2dmu\u4e3a\u5747\u503c\uff0csigma\u4e3a\u6807\u51c6\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_gauss = random.gauss(0, 1)<\/p>\n<p>print(random_gauss)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u751f\u6210\u8303\u56f4\u5185\u7684\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u751f\u6210\u7279\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\uff0crandom\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528choice()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>choice(seq)<\/code>\u51fd\u6570\u4ece\u5e8f\u5217seq\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002seq\u53ef\u4ee5\u662f\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_choice = random.choice([1, 2, 3, 4, 5])<\/p>\n<p>print(random_choice)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528sample()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>sample(population, k)<\/code>\u51fd\u6570\u4ecepopulation\u4e2d\u968f\u673a\u9009\u62e9k\u4e2a\u5143\u7d20\uff0c\u5e76\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_sample = random.sample(range(1, 10), 3)<\/p>\n<p>print(random_sample)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528shuffle()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>shuffle(seq)<\/code>\u51fd\u6570\u5c06\u5e8f\u5217seq\u4e2d\u7684\u5143\u7d20\u968f\u673a\u6253\u4e71\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>random.shuffle(my_list)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u751f\u6210\u968f\u673a\u79cd\u5b50<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u8bc1\u968f\u673a\u6570\u7684\u53ef\u91cd\u590d\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>seed()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u968f\u673a\u79cd\u5b50\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random.seed(10)<\/p>\n<p>print(random.random())<\/p>\n<p>print(random.random())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u751f\u6210\u968f\u673a\u5e03\u5c14\u503c<\/h3>\n<\/p>\n<p><p>random\u6a21\u5757\u8fd8\u53ef\u4ee5\u751f\u6210\u968f\u673a\u5e03\u5c14\u503c\uff0c\u4f7f\u7528<code>choice()<\/code>\u51fd\u6570\u4ece[True, False]\u4e2d\u968f\u673a\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_bool = random.choice([True, False])<\/p>\n<p>print(random_bool)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u751f\u6210\u52a0\u6743\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u751f\u6210\u52a0\u6743\u968f\u673a\u6570\uff0crandom\u6a21\u5757\u7684<code>choices()<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_weighted = random.choices([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], weights=[10, 1, 1], k=5)<\/p>\n<p>print(random_weighted)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u751f\u6210\u968f\u673a\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u968f\u673a\u5b57\u7b26\u4e32\u5728\u5f88\u591a\u573a\u666f\u4e0b\u90fd\u662f\u5fc5\u8981\u7684\uff0c\u6bd4\u5982\u751f\u6210\u968f\u673a\u5bc6\u7801\u3002\u53ef\u4ee5\u4f7f\u7528<code>string<\/code>\u6a21\u5757\u548c<code>random<\/code>\u6a21\u5757\u7ed3\u5408\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import string<\/p>\n<p>random_string = &#39;&#39;.join(random.choices(string.ascii_letters + string.digits, k=10))<\/p>\n<p>print(random_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u751f\u6210\u968f\u673a\u65e5\u671f\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u968f\u673a\u65e5\u671f\u65f6\u95f4\u4e5f\u5f88\u5e38\u89c1\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u548c<code>random<\/code>\u6a21\u5757\u7ed3\u5408\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>from datetime import datetime, timedelta<\/p>\n<p>def random_date(start, end):<\/p>\n<p>    delta = end - start<\/p>\n<p>    int_delta = delta.days * 24 * 60 * 60<\/p>\n<p>    random_second = random.randrange(int_delta)<\/p>\n<p>    return start + timedelta(seconds=random_second)<\/p>\n<p>start_date = datetime(2020, 1, 1)<\/p>\n<p>end_date = datetime(2021, 1, 1)<\/p>\n<p>print(random_date(start_date, end_date))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u751f\u6210\u52a0\u5bc6\u5b89\u5168\u7684\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>secrets<\/code>\u6a21\u5757\u6765\u751f\u6210\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>secure_random = secrets.randbelow(100)<\/p>\n<p>print(secure_random)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u7684random\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u751f\u6210\u6240\u9700\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\u4e00\u4e2a\u7279\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u7684<code>randint<\/code>\u51fd\u6570\u6765\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u8c03\u7528<code>random.randint(a, b)<\/code>\uff0c\u5176\u4e2d<code>a<\/code>\u548c<code>b<\/code>\u5206\u522b\u662f\u8303\u56f4\u7684\u4e0b\u9650\u548c\u4e0a\u9650\uff0c\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u5728\u8fd9\u4e2a\u8303\u56f4\u5185\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>\u5c06\u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u9664\u4e86\u751f\u6210\u968f\u673a\u6574\u6570\uff0cPython\u7684<code>random<\/code>\u6a21\u5757\u540c\u6837\u652f\u6301\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u3002\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5728<code>a<\/code>\u548c<code>b<\/code>\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd4\u56de\u7684\u503c\u5305\u62ec<code>a<\/code>\u4f46\u4e0d\u4e00\u5b9a\u5305\u62ec<code>b<\/code>\uff0c\u4f8b\u5982\uff0c<code>random.uniform(1.0, 10.0)<\/code>\u5c06\u751f\u6210\u4e00\u4e2a\u57281.0\u523010.0\u4e4b\u95f4\u7684\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\u5e76\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>random.randint()<\/code>\u6216<code>random.uniform()<\/code>\u3002\u4f8b\u5982\uff0c\u751f\u62105\u4e2a1\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<code>random_numbers = [random.randint(1, 100) for _ in range(5)]<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u4e00\u4e2a\u6b65\u9aa4\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u968f\u673a\u6570\u7684\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528random\u6a21\u5757\u3001\u751f\u6210\u968f\u673a\u6574\u6570\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u3001\u751f\u6210\u8303\u56f4 [&hellip;]","protected":false},"author":3,"featured_media":1065441,"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\/1065437"}],"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=1065437"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065437\/revisions"}],"predecessor-version":[{"id":1065443,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065437\/revisions\/1065443"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1065441"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1065437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1065437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1065437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}