{"id":1094312,"date":"2025-01-08T14:39:49","date_gmt":"2025-01-08T06:39:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094312.html"},"modified":"2025-01-08T14:39:51","modified_gmt":"2025-01-08T06:39:51","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e9%87%8c%e5%81%9a%e5%88%b0%e9%9a%8f%e6%9c%ba%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094312.html","title":{"rendered":"\u5982\u4f55\u5728python\u91cc\u505a\u5230\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\/24210458\/b2f917a2-ece0-44af-8d1a-49be028d252b.webp\" alt=\"\u5982\u4f55\u5728python\u91cc\u505a\u5230\u968f\u673a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528random\u6a21\u5757\u3001\u4f7f\u7528numpy\u6a21\u5757\u3001\u4f7f\u7528secrets\u6a21\u5757<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528random\u6a21\u5757\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u800cnumpy\u6a21\u5757\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u5927\u6570\u636e\u5904\u7406\uff0csecrets\u6a21\u5757\u5219\u7528\u4e8e\u751f\u6210\u5b89\u5168\u968f\u673a\u6570\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6570\uff0c\u5e76\u5bf9\u4f7f\u7528random\u6a21\u5757\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528random\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>random\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u751f\u6210\u968f\u673a\u6570\u7684\u51fd\u6570\u3002<\/strong><\/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()<\/code>\u51fd\u6570\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<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(random_float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>randint(a, b)<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u4f4d\u4e8ea\u548cb\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u62eca\u548cb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/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(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>uniform(a, b)<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u4f4d\u4e8ea\u548cb\u4e4b\u95f4\u7684\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\u4e2a1.0\u523010.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_uniform = random.uniform(1.0, 10.0)<\/p>\n<p>print(random_uniform)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>choice(seq)<\/code>\u51fd\u6570\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>random_choice = random.choice(my_list)<\/p>\n<p>print(random_choice)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u751f\u6210\u6307\u5b9a\u6570\u91cf\u7684\u968f\u673a\u6837\u672c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>sample(population, k)<\/code>\u51fd\u6570\u53ef\u4ee5\u4ece\u603b\u6570\u4e3apopulation\u7684\u96c6\u5408\u4e2d\u751f\u6210k\u4e2a\u968f\u673a\u6837\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e93\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>random_sample = random.sample(my_list, 3)<\/p>\n<p>print(random_sample)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6\u3001\u6253\u4e71\u5e8f\u5217\u7684\u987a\u5e8f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>shuffle(seq)<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u6253\u4e71\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u6253\u4e71\u5217\u8868\u4e2d\u7684\u5143\u7d20\u987a\u5e8f<\/strong><\/h2>\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>\u4e8c\u3001\u4f7f\u7528numpy\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>numpy\u6a21\u5757\u662fPython\u4e2d\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u51fd\u6570\u3002<\/strong><\/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>\u51fd\u6570\u53ef\u4ee5\u751f\u62100\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53c2\u6570d0, d1, &#8230;, dn\u8868\u793a\u751f\u6210\u7684\u6570\u7ec4\u7684\u5f62\u72b6\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(random_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.random.randint(low, high=None, size=None, dtype=int)<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u53c2\u6570low\u8868\u793a\u6700\u4f4e\u503c\uff0chigh\u8868\u793a\u6700\u9ad8\u503c\uff0csize\u8868\u793a\u751f\u6210\u7684\u6570\u7ec4\u7684\u5f62\u72b6\uff0cdtype\u8868\u793a\u6570\u636e\u7c7b\u578b\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\u6574\u6570\u6570\u7ec4\uff0c\u8303\u56f4\u57281\u523010\u4e4b\u95f4<\/strong><\/h2>\n<p>random_int_array = np.random.randint(1, 10, size=(3, 3))<\/p>\n<p>print(random_int_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.random.normal(loc=0.0, scale=1.0, size=None)<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\uff0c\u53c2\u6570loc\u8868\u793a\u5747\u503c\uff0cscale\u8868\u793a\u6807\u51c6\u5dee\uff0csize\u8868\u793a\u751f\u6210\u7684\u6570\u7ec4\u7684\u5f62\u72b6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a3x3\u7684\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\u6570\u7ec4\uff0c\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a1<\/strong><\/h2>\n<p>random_normal_array = np.random.normal(0, 1, size=(3, 3))<\/p>\n<p>print(random_normal_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528secrets\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>secrets\u6a21\u5757\u662fPython 3.6\u5f15\u5165\u7684\uff0c\u7528\u4e8e\u751f\u6210\u5b89\u5168\u968f\u673a\u6570\uff0c\u9002\u7528\u4e8e\u5bc6\u7801\u5b66\u7528\u9014\u3002<\/strong><\/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>\u51fd\u6570\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>secure_random_int = secrets.randbelow(10)<\/p>\n<p>print(secure_random_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=None)<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5305\u542bnbytes\u5b57\u8282\u7684\u968f\u673a\u5b57\u8282\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5305\u542b16\u4e2a\u5b57\u8282\u7684\u968f\u673a\u5b57\u8282\u4e32<\/strong><\/h2>\n<p>secure_random_bytes = secrets.token_bytes(16)<\/p>\n<p>print(secure_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>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2aURL\u5b89\u5168\u7684\u968f\u673a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2aURL\u5b89\u5168\u7684\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5305\u542b16\u4e2a\u5b57\u8282<\/strong><\/h2>\n<p>secure_random_urlsafe = secrets.token_urlsafe(16)<\/p>\n<p>print(secure_random_urlsafe)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u4f8b\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u968f\u673a\u9a8c\u8bc1\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u9a8c\u8bc1\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import string<\/p>\n<p>def generate_verification_code(length=6):<\/p>\n<p>    # \u5b57\u7b26\u96c6\uff0c\u5305\u62ec\u6570\u5b57\u548c\u5b57\u6bcd<\/p>\n<p>    characters = string.ascii_letters + string.digits<\/p>\n<p>    # \u4ece\u5b57\u7b26\u96c6\u4e2d\u968f\u673a\u9009\u62e9length\u4e2a\u5b57\u7b26\u7ec4\u6210\u9a8c\u8bc1\u7801<\/p>\n<p>    verification_code = &#39;&#39;.join(random.choice(characters) for _ in range(length))<\/p>\n<p>    return verification_code<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a6\u4f4d\u7684\u968f\u673a\u9a8c\u8bc1\u7801<\/strong><\/h2>\n<p>verification_code = generate_verification_code()<\/p>\n<p>print(verification_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u968f\u673a\u5bc6\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528secrets\u6a21\u5757\u751f\u6210\u5b89\u5168\u7684\u968f\u673a\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<p>import string<\/p>\n<p>def generate_secure_password(length=12):<\/p>\n<p>    # \u5b57\u7b26\u96c6\uff0c\u5305\u62ec\u6570\u5b57\u3001\u5b57\u6bcd\u548c\u7279\u6b8a\u5b57\u7b26<\/p>\n<p>    characters = string.ascii_letters + string.digits + string.punctuation<\/p>\n<p>    # \u4ece\u5b57\u7b26\u96c6\u4e2d\u968f\u673a\u9009\u62e9length\u4e2a\u5b57\u7b26\u7ec4\u6210\u5bc6\u7801<\/p>\n<p>    secure_password = &#39;&#39;.join(secrets.choice(characters) for _ in range(length))<\/p>\n<p>    return secure_password<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a12\u4f4d\u7684\u968f\u673a\u5bc6\u7801<\/strong><\/h2>\n<p>secure_password = generate_secure_password()<\/p>\n<p>print(secure_password)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6a21\u62df\u63b7\u9ab0\u5b50<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528numpy\u6a21\u5757\u6a21\u62df\u63b7\u9ab0\u5b50\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def roll_dice(num_rolls=10):<\/p>\n<p>    # \u6a21\u62df\u63b7\u9ab0\u5b50\uff0c\u751f\u6210num_rolls\u4e2a1\u52306\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/p>\n<p>    dice_rolls = np.random.randint(1, 7, size=num_rolls)<\/p>\n<p>    return dice_rolls<\/p>\n<h2><strong>\u6a21\u62df\u63b710\u6b21\u9ab0\u5b50<\/strong><\/h2>\n<p>dice_rolls = roll_dice()<\/p>\n<p>print(dice_rolls)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u751f\u6210\u968f\u673a\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def generate_random_color():<\/p>\n<p>    # \u751f\u6210RGB\u4e09\u4e2a\u901a\u9053\u7684\u968f\u673a\u503c<\/p>\n<p>    r = random.randint(0, 255)<\/p>\n<p>    g = random.randint(0, 255)<\/p>\n<p>    b = random.randint(0, 255)<\/p>\n<p>    return (r, g, b)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u989c\u8272<\/strong><\/h2>\n<p>random_color = generate_random_color()<\/p>\n<p>print(random_color)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u751f\u6210\u968f\u673a\u65e5\u671f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u65e5\u671f\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 generate_random_date(start_date, end_date):<\/p>\n<p>    # \u8ba1\u7b97\u65e5\u671f\u8303\u56f4\u5185\u7684\u603b\u5929\u6570<\/p>\n<p>    delta_days = (end_date - start_date).days<\/p>\n<p>    # \u751f\u6210\u4e00\u4e2a\u968f\u673a\u5929\u6570<\/p>\n<p>    random_days = random.randint(0, delta_days)<\/p>\n<p>    # \u8ba1\u7b97\u968f\u673a\u65e5\u671f<\/p>\n<p>    random_date = start_date + timedelta(days=random_days)<\/p>\n<p>    return random_date<\/p>\n<h2><strong>\u5b9a\u4e49\u5f00\u59cb\u65e5\u671f\u548c\u7ed3\u675f\u65e5\u671f<\/strong><\/h2>\n<p>start_date = datetime(2021, 1, 1)<\/p>\n<p>end_date = datetime(2021, 12, 31)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u65e5\u671f<\/strong><\/h2>\n<p>random_date = generate_random_date(start_date, end_date)<\/p>\n<p>print(random_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u968f\u673a\u6570\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u968f\u673a\u6570\u751f\u6210\uff0c\u8fd8\u662f\u7528\u4e8e\u5bc6\u7801\u5b66\u7684\u5b89\u5168\u968f\u673a\u6570\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u4e2d\u7684\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\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\u6570\u7684\u57fa\u672c\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u751f\u6210\u968f\u673a\u6570\u901a\u5e38\u4f7f\u7528\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u3002\u901a\u8fc7\u8c03\u7528<code>random.random()<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u57280\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u82e5\u9700\u8981\u751f\u6210\u7279\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\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\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>random.sample()<\/code>\u6216<code>random.choices()<\/code>\u6765\u751f\u6210\u591a\u4e2a\u968f\u673a\u6570\u3002<code>random.sample()<\/code>\u4ece\u6307\u5b9a\u8303\u56f4\u4e2d\u968f\u673a\u9009\u62e9\u552f\u4e00\u7684\u5143\u7d20\uff0c\u800c<code>random.choices()<\/code>\u5219\u5141\u8bb8\u9009\u62e9\u91cd\u590d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>random.sample(range(1, 100), 10)<\/code>\u5c06\u4ece1\u523099\u4e4b\u95f4\u9009\u62e910\u4e2a\u4e0d\u540c\u7684\u968f\u673a\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5728Python\u4e2d\u751f\u6210\u7684\u968f\u673a\u6570\u662f\u53ef\u91cd\u73b0\u7684\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u6570\u662f\u53ef\u91cd\u73b0\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.seed()<\/code>\u65b9\u6cd5\u3002\u901a\u8fc7\u8bbe\u7f6e\u79cd\u5b50\u503c\uff0c\u540e\u7eed\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u5c06\u4fdd\u6301\u4e00\u81f4\u3002\u4f8b\u5982\uff0c\u8c03\u7528<code>random.seed(42)<\/code>\u540e\uff0c\u518d\u8c03\u7528\u751f\u6210\u968f\u673a\u6570\u7684\u51fd\u6570\u65f6\uff0c\u6bcf\u6b21\u8fd0\u884c\u90fd\u4f1a\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u8fd9\u5728\u8c03\u8bd5\u6216\u6d4b\u8bd5\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528random\u6a21\u5757\u3001\u4f7f\u7528numpy\u6a21\u5757\u3001\u4f7f\u7528secrets\u6a21\u5757\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1094316,"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\/1094312"}],"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=1094312"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094312\/revisions"}],"predecessor-version":[{"id":1094318,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094312\/revisions\/1094318"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094316"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}