{"id":1068952,"date":"2024-12-31T16:47:44","date_gmt":"2024-12-31T08:47:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068952.html"},"modified":"2024-12-31T16:47:47","modified_gmt":"2024-12-31T08:47:47","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e4%b8%80%e4%b8%aa%e9%9a%8f%e6%9c%ba%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1068952.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/517947b2-e341-45f3-8900-1ff69893115e.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u3001secrets\u6a21\u5757\u548cnumpy\u5e93\u7b49\u3002<\/strong> \u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u8be6\u7ec6\u8bf4\u660e\u6bcf\u4e2a\u65b9\u6cd5\u7684\u4f7f\u7528\u7ec6\u8282\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528random\u6a21\u5757<\/p>\n<\/p>\n<p><p>random\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u4e13\u95e8\u7528\u4e8e\u751f\u6210\u4f2a\u968f\u673a\u6570\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u751f\u6210\u4e0d\u540c\u7c7b\u578b\u7684\u968f\u673a\u6570\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u548c\u5e8f\u5217\u4e2d\u7684\u968f\u673a\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>1. \u751f\u6210\u968f\u673a\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u968f\u673a\u6574\u6570\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528random.randint(a, b)\u65b9\u6cd5\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\">import random<\/p>\n<p>random_integer = random.randint(1, 10)<\/p>\n<p>print(f&quot;Random Integer: {random_integer}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0crandom.randint(1, 10)\u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>2. \u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p>\u8981\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528random.uniform(a, b)\u65b9\u6cd5\uff0c\u5b83\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_float = random.uniform(1.0, 10.0)<\/p>\n<p>print(f&quot;Random Float: {random_float}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>random.uniform(1.0, 10.0)\u751f\u6210\u4e00\u4e2a\u57281.0\u548c10.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>3. \u751f\u6210\u968f\u673a\u5e8f\u5217\u5143\u7d20<\/h3>\n<\/p>\n<p><p>random.choice(seq)\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u4ece\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_choice = random.choice([&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;])<\/p>\n<p>print(f&quot;Random Choice: {random_choice}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>random.choice([&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;])\u5c06\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>4. \u751f\u6210\u968f\u673a\u5e8f\u5217<\/h3>\n<\/p>\n<p><p>random.sample(population, k)\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u4ece\u6307\u5b9a\u7684\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9k\u4e2a\u5143\u7d20\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u968f\u673a\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_sample = random.sample(range(1, 100), 5)<\/p>\n<p>print(f&quot;Random Sample: {random_sample}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>random.sample(range(1, 100), 5)\u4ece1\u523099\u4e4b\u95f4\u968f\u673a\u9009\u62e95\u4e2a\u4e0d\u540c\u7684\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>5. \u6d17\u724c<\/h3>\n<\/p>\n<p><p>random.shuffle(x)\u65b9\u6cd5\u7528\u4e8e\u5c06\u5e8f\u5217\u7684\u5143\u7d20\u968f\u673a\u6253\u4e71\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">deck = list(range(1, 53))  # \u751f\u6210\u4e00\u4e2a1\u523052\u7684\u5217\u8868\uff0c\u6a21\u62df\u4e00\u526f\u6251\u514b\u724c<\/p>\n<p>random.shuffle(deck)<\/p>\n<p>print(f&quot;Shuffled Deck: {deck}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>random.shuffle(deck)\u5c06\u5217\u8868deck\u4e2d\u7684\u5143\u7d20\u987a\u5e8f\u968f\u673a\u6253\u4e71\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528secrets\u6a21\u5757<\/p>\n<\/p>\n<p><p>secrets\u6a21\u5757\u662fPython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u65b0\u589e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u751f\u6210\u52a0\u5bc6\u5f3a\u5ea6\u7684\u968f\u673a\u6570\u3002\u5b83\u9002\u7528\u4e8e\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u5982\u751f\u6210\u5bc6\u7801\u3001\u4ee4\u724c\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u751f\u6210\u5b89\u5168\u7684\u968f\u673a\u6574\u6570<\/h3>\n<\/p>\n<p><p>secrets.randbelow(n)\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u57280\u5230n-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_integer = secrets.randbelow(10)<\/p>\n<p>print(f&quot;Secure Random Integer: {secure_random_integer}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>secrets.randbelow(10)\u751f\u6210\u4e00\u4e2a0\u52309\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>2. \u751f\u6210\u5b89\u5168\u7684\u968f\u673a\u5b57\u8282<\/h3>\n<\/p>\n<p><p>secrets.token_bytes(n)\u65b9\u6cd5\u751f\u6210\u4e00\u4e2a\u5305\u542bn\u4e2a\u968f\u673a\u5b57\u8282\u7684\u5b57\u7b26\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;Secure Random Bytes: {secure_random_bytes}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>secrets.token_bytes(16)\u751f\u6210\u4e00\u4e2a\u5305\u542b16\u4e2a\u968f\u673a\u5b57\u8282\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>3. \u751f\u6210\u5b89\u5168\u7684\u968f\u673aURL\u5b89\u5168\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>secrets.token_url<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>(n)\u65b9\u6cd5\u751f\u6210\u4e00\u4e2aURL\u5b89\u5168\u7684\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u81f3\u5c11\u4e3an\u4e2a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">secure_random_urlsafe = secrets.token_urlsafe(16)<\/p>\n<p>print(f&quot;Secure Random URL Safe String: {secure_random_urlsafe}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>secrets.token_urlsafe(16)\u751f\u6210\u4e00\u4e2a\u957f\u5ea6\u81f3\u5c11\u4e3a16\u4e2a\u5b57\u8282\u7684URL\u5b89\u5168\u968f\u673a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528numpy\u5e93<\/p>\n<\/p>\n<p><p>numpy\u5e93\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u6269\u5c55\u5e93\uff0c\u5176\u4e2d\u4e5f\u5305\u542b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u3002\u5b83\u66f4\u9002\u7528\u4e8e\u9700\u8981\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>1. \u751f\u6210\u968f\u673a\u6574\u6570\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528numpy.random.randint(low, high, size)\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5305\u542b\u968f\u673a\u6574\u6570\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>random_integers = np.random.randint(1, 10, size=5)<\/p>\n<p>print(f&quot;Random Integers Array: {random_integers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>np.random.randint(1, 10, size=5)\u751f\u6210\u4e00\u4e2a\u5305\u542b5\u4e2a1\u52309\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>2. \u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>numpy.random.uniform(low, high, size)\u65b9\u6cd5\u751f\u6210\u4e00\u4e2a\u5305\u542b\u968f\u673a\u6d6e\u70b9\u6570\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_floats = np.random.uniform(1.0, 10.0, size=5)<\/p>\n<p>print(f&quot;Random Floats Array: {random_floats}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>np.random.uniform(1.0, 10.0, size=5)\u751f\u6210\u4e00\u4e2a\u5305\u542b5\u4e2a1.0\u523010.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>3. \u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570<\/h3>\n<\/p>\n<p><p>numpy.random.normal(loc, scale, size)\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u7b26\u5408\u6b63\u6001\u5206\u5e03\uff08\u9ad8\u65af\u5206\u5e03\uff09\u7684\u968f\u673a\u6570\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_normals = np.random.normal(loc=0, scale=1, size=5)<\/p>\n<p>print(f&quot;Random Normal Distribution: {random_normals}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>np.random.normal(loc=0, scale=1, size=5)\u751f\u6210\u4e00\u4e2a\u5305\u542b5\u4e2a\u7b26\u5408\u6807\u51c6\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><h3>1. \u968f\u673a\u5bc6\u7801\u751f\u6210<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u968f\u673a\u5bc6\u7801\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u8d26\u6237\u6ce8\u518c\u548c\u5bc6\u7801\u91cd\u7f6e\u7684\u573a\u666f\u4e0b\u3002\u53ef\u4ee5\u7ed3\u5408secrets\u6a21\u5757\u548c\u5b57\u7b26\u4e32\u64cd\u4f5c\u751f\u6210\u4e00\u4e2a\u5b89\u5168\u7684\u968f\u673a\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import string<\/p>\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>random_password = generate_password()<\/p>\n<p>print(f&quot;Random Password: {random_password}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>generate_password()\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u7279\u6b8a\u5b57\u7b26\u7684\u968f\u673a\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><h3>2. \u968f\u673a\u6837\u672c\u62bd\u53d6<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u968f\u673a\u6837\u672c\u62bd\u53d6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528random\u6a21\u5757\u6216numpy\u5e93\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = list(range(100))  # \u5047\u8bbe\u6570\u636e\u96c6\u5305\u542b0\u523099\u7684\u6570\u636e<\/p>\n<h2><strong>\u4f7f\u7528random\u6a21\u5757\u62bd\u53d6\u6837\u672c<\/strong><\/h2>\n<p>sample_size = 10<\/p>\n<p>random_sample = random.sample(data, sample_size)<\/p>\n<p>print(f&quot;Random Sample (random module): {random_sample}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u5e93\u62bd\u53d6\u6837\u672c<\/strong><\/h2>\n<p>random_sample_np = np.random.choice(data, sample_size, replace=False)<\/p>\n<p>print(f&quot;Random Sample (numpy): {random_sample_np}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528random\u6a21\u5757\u548cnumpy\u5e93\u4ece\u6570\u636e\u96c6\u4e2d\u62bd\u53d6\u968f\u673a\u6837\u672c\u3002<\/p>\n<\/p>\n<p><h3>3. \u6a21\u62df\u5b9e\u9a8c<\/h3>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u7814\u7a76\u548c\u5de5\u7a0b\u5e94\u7528\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u901a\u8fc7\u6a21\u62df\u5b9e\u9a8c\u6765\u9a8c\u8bc1\u7406\u8bba\u6216\u9884\u6d4b\u7ed3\u679c\u3002\u968f\u673a\u6570\u751f\u6210\u5728\u6a21\u62df\u5b9e\u9a8c\u4e2d\u8d77\u7740\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def monte_carlo_pi(num_samples=10000):<\/p>\n<p>    inside_circle = 0<\/p>\n<p>    for _ in range(num_samples):<\/p>\n<p>        x, y = random.uniform(-1, 1), random.uniform(-1, 1)<\/p>\n<p>        if x&lt;strong&gt;2 + y&lt;\/strong&gt;2 &lt;= 1:<\/p>\n<p>            inside_circle += 1<\/p>\n<p>    return (inside_circle \/ num_samples) * 4<\/p>\n<p>estimated_pi = monte_carlo_pi()<\/p>\n<p>print(f&quot;Estimated Pi: {estimated_pi}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>monte_carlo_pi()\u51fd\u6570\u4f7f\u7528\u8499\u7279\u5361\u7f57\u65b9\u6cd5\u4f30\u8ba1\u5706\u5468\u7387\u03c0\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>4. \u968f\u673a\u6570\u636e\u751f\u6210<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u548c\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u751f\u6210\u968f\u673a\u6570\u636e\u6765\u6d4b\u8bd5\u7b97\u6cd5\u6216\u6a21\u62df\u6570\u636e\u5206\u5e03\u3002numpy\u5e93\u975e\u5e38\u9002\u5408\u8fd9\u79cd\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u968f\u673a\u6570\u636e\u96c6<\/p>\n<p>num_samples = 100<\/p>\n<p>features = 5<\/p>\n<p>random_data = np.random.rand(num_samples, features)<\/p>\n<p>print(f&quot;Random Data:\\n{random_data}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b100\u4e2a\u6837\u672c\u548c5\u4e2a\u7279\u5f81\u7684\u968f\u673a\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>random\u6a21\u5757\u9002\u7528\u4e8e\u4e00\u822c\u7528\u9014\u7684\u968f\u673a\u6570\u751f\u6210\uff0csecrets\u6a21\u5757\u9002\u7528\u4e8e\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0cnumpy\u5e93\u5219\u9002\u7528\u4e8e\u9700\u8981\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\u7684\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u573a\u666f\u3002<\/strong> \u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u7075\u6d3b\u751f\u6210\u968f\u673a\u6570\uff0c\u6ee1\u8db3\u5404\u79cd\u9700\u6c42\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\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u751f\u6210\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\u3002\u901a\u8fc7<code>random.randint(a, b)<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5305\u542b<code>a<\/code>\u548c<code>b<\/code>\u7684\u968f\u673a\u6574\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import random\nrandom_number = random.randint(1, 100)  # \u751f\u62101\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\nprint(random_number)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u6570\u603b\u662f\u5728\u6307\u5b9a\u7684\u8303\u56f4\u5185\u3002<\/p>\n<p><strong>Python\u4e2d\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u6709\u54ea\u51e0\u79cd\u7c7b\u578b\uff1f<\/strong><br \/>Python\u7684<code>random<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\uff0c\u5305\u62ec\uff1a  <\/p>\n<ol>\n<li><code>random.random()<\/code>\uff1a\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u6d6e\u52a8\u968f\u673a\u6570\u3002  <\/li>\n<li><code>random.randint(a, b)<\/code>\uff1a\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002  <\/li>\n<li><code>random.uniform(a, b)<\/code>\uff1a\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6d6e\u52a8\u968f\u673a\u6570\u3002  <\/li>\n<li><code>random.choice(sequence)<\/code>\uff1a\u4ece\u7ed9\u5b9a\u7684\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002<br \/>\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u4f7f\u7528\u3002<\/li>\n<\/ol>\n<p><strong>\u5982\u4f55\u907f\u514dPython\u4e2d\u968f\u673a\u6570\u751f\u6210\u7684\u91cd\u590d\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u907f\u514d\u751f\u6210\u91cd\u590d\u7684\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u5b58\u50a8\u5df2\u7ecf\u751f\u6210\u7684\u968f\u673a\u6570\u3002\u5728\u751f\u6210\u65b0\u968f\u673a\u6570\u4e4b\u524d\uff0c\u68c0\u67e5\u8be5\u968f\u673a\u6570\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e8e\u96c6\u5408\u4e2d\u3002\u5982\u679c\u5b58\u5728\uff0c\u5219\u91cd\u65b0\u751f\u6210\uff0c\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u65b0\u7684\u968f\u673a\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import random\n\nunique_numbers = set()\nwhile len(unique_numbers) &lt; 10:  # \u751f\u621010\u4e2a\u552f\u4e00\u7684\u968f\u673a\u6570\n    num = random.randint(1, 100)\n    unique_numbers.add(num)\nprint(unique_numbers)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u6570\u90fd\u662f\u552f\u4e00\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u3001secrets\u6a21\u5757\u548cnumpy\u5e93\u7b49\u3002 \u672c\u6587\u5c06 [&hellip;]","protected":false},"author":3,"featured_media":1068960,"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\/1068952"}],"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=1068952"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068952\/revisions"}],"predecessor-version":[{"id":1068961,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068952\/revisions\/1068961"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068960"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}