{"id":316376,"date":"2024-05-21T17:20:37","date_gmt":"2024-05-21T09:20:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/316376.html"},"modified":"2024-05-21T17:21:14","modified_gmt":"2024-05-21T09:21:14","slug":"rand%e4%b8%8esrand%e6%9c%89%e5%95%a5%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/316376.html","title":{"rendered":"rand\u4e0esrand\u6709\u5565\u533a\u522b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26075722\/3d424177-c080-49b6-ad0b-b3998f43723a.webp\" alt=\"rand\u4e0esrand\u6709\u5565\u533a\u522b\" \/><\/p>\n<p><p><strong>rand<\/strong> \u51fd\u6570\u662f\u7528\u4e8e\u751f\u6210\u4f2a\u968f\u673a\u6570\u7684\u3001\u800c <strong>srand<\/strong> \u662f\u4e00\u4e2a\u7528\u6765\u8bbe\u7f6e rand \u51fd\u6570\u4ea7\u751f\u968f\u673a\u6570\u5e8f\u5217\u79cd\u5b50\u7684\u51fd\u6570\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u901a\u5e38\u4e00\u8d77\u7528\u4e8e\u7a0b\u5e8f\u4e2d\u4ee5\u4ea7\u751f\u770b\u8d77\u6765\u662f\u968f\u673a\u7684\u6570\u5b57\u3002\u8981\u83b7\u5f97\u4e0d\u540c\u7684\u968f\u673a\u5e8f\u5217\uff0c\u5fc5\u987b\u901a\u8fc7 srand \u4f7f\u7528\u4e0d\u540c\u7684\u79cd\u5b50\u503c\uff0c\u901a\u5e38\u4f7f\u7528\u5f53\u524d\u65f6\u95f4\u4f5c\u4e3a\u79cd\u5b50\u503c\uff0c\u56e0\u4e3a\u5b83\u662f\u4e0d\u65ad\u53d8\u5316\u7684\u3002<\/p>\n<\/p>\n<p><p>srand \u51fd\u6570\u5e94\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u8c03\u7528\u4e00\u6b21\uff0c\u5982\u679c\u4e0d\u663e\u5f0f\u8bbe\u7f6e\u79cd\u5b50\uff0crand \u51fd\u6570\u5c06\u9ed8\u8ba4\u4ee5\u79cd\u5b50\u6570\u5b571\u521d\u59cb\u5316\uff0c\u7ed3\u679c\u5bfc\u81f4\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u90fd\u662f\u76f8\u540c\u7684\u3002\u901a\u8fc7\u4f7f\u7528 srand \u8bbe\u7f6e\u4e0d\u540c\u7684\u79cd\u5b50\u503c\uff0c\u53ef\u4ee5\u786e\u4fdd\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\u4ea7\u751f\u4e0d\u540c\u7684\u968f\u673a\u6570\u5e8f\u5217\uff0c\u63d0\u5347\u4e86\u968f\u673a\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u968f\u673a\u6570\u751f\u6210\u57fa\u7840<\/h3>\n<\/p>\n<p><p>\u968f\u673a\u6570\u5728\u7f16\u7a0b\u4e2d\u6709\u8bb8\u591a\u5e94\u7528\uff0c\u5305\u62ec\u6a21\u62df\u3001\u6e38\u620f\u3001\u5b89\u5168\u7b97\u6cd5\u548c\u6d4b\u8bd5\u4e2d\u3002\u968f\u673a\u6570\u751f\u6210\u5668\uff08RNG\uff09\u5206\u4e3a\u4e24\u7c7b\uff1a\u771f\u968f\u673a\u6570\u751f\u6210\u5668\u548c\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\u3002<\/p>\n<\/p>\n<p><h4>\u751f\u6210\u4f2a\u968f\u673a\u6570<\/h4>\n<\/p>\n<p><p>\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\uff08PRNG\uff09\u4f7f\u7528\u7b97\u672f\u8fd0\u7b97\u6765\u751f\u6210\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u8fd9\u4e9b\u6570\u5b57\u867d\u7136\u4e0d\u662f\u771f\u6b63\u968f\u673a\u7684\uff0c\u4f46\u5177\u6709\u968f\u673a\u6570\u7684\u7edf\u8ba1\u7279\u6027\u3002rand \u51fd\u6570\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u7684PRNG\uff0c\u5b83\u53ef\u4ee5\u5728\u7ed9\u5b9a\u8303\u56f4\u5185\u4ea7\u751f\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u521d\u59cb\u5316\u968f\u673a\u6570\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u751f\u6210\u968f\u673a\u6570\u4e4b\u524d\uff0cPRNG\u9700\u8981\u4e00\u4e2a\u8d77\u59cb\u70b9\uff0c\u8fd9\u4e2a\u8d77\u59cb\u70b9\u79f0\u4e3a\u201c\u79cd\u5b50\u201d\u503c\u3002srand \u51fd\u6570\u7684\u4f5c\u7528\u5c31\u662f\u63d0\u4f9b\u8fd9\u4e2a\u79cd\u5b50\u3002\u79cd\u5b50\u503c\u6539\u53d8\uff0c\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u4e5f\u4f1a\u968f\u4e4b\u6539\u53d8\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6df1\u5165\u7406\u89e3 rand \u51fd\u6570<\/h3>\n<\/p>\n<p><h4>rand \u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406<\/h4>\n<\/p>\n<p><p>rand \u51fd\u6570\u662f C \u6807\u51c6\u5e93\u4e2d\u7528\u6765\u751f\u6210\u4e00\u4e2a\u4f2a\u968f\u673a\u6570\u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e0\u5230RAND_MAX\u4e4b\u95f4\u7684\u6574\u6570\u3002RAND_MAX\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u5b83\u7684\u503c\u5728\u4e0d\u540c\u7684\u7cfb\u7edf\u4e2d\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u81f3\u5c11\u662f32767\u3002<\/p>\n<\/p>\n<p><h4>rand \u51fd\u6570\u7684\u7528\u9014<\/h4>\n<\/p>\n<p><p>\u5728\u9700\u8981\u968f\u673a\u6027\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528rand\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5b83\u7ecf\u5e38\u88ab\u7528\u5728\u6e38\u620f\u7684\u968f\u673a\u4e8b\u4ef6\u51b3\u7b56\u3001\u968f\u673a\u5316\u7b97\u6cd5\u6d4b\u8bd5\u6216\u6a21\u62df\u7b49\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6df1\u5165\u7406\u89e3 srand \u51fd\u6570<\/h3>\n<\/p>\n<p><h4>\u8bbe\u7f6e\u968f\u673a\u79cd\u5b50<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 srand \u51fd\u6570\u53ef\u4ee5\u7ed9 rand \u51fd\u6570\u8bbe\u7f6e\u4e00\u4e2a\u79cd\u5b50\u503c\u3002\u79cd\u5b50\u503c\u4e00\u822c\u9009\u62e9\u4e00\u4e2a\u968f\u65f6\u53d8\u5316\u7684\u6570\uff0c<\/p>\n<p>\u4f8b\u5982\u5f53\u524d\u65f6\u95f4\uff0c\u6765\u4fdd\u8bc1\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u90fd\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h4>srand \u51fd\u6570\u7684\u5178\u578b\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4e00\u4e2a\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u7ed3\u5408 time \u51fd\u6570\u4f7f\u7528 srand\uff0c\u4f8b\u5982 <code>srand((unsigned) time(NULL));<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u968f\u673a\u6027\u7684\u6700\u5927\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001 rand \u548c srand \u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u9645\u4ee3\u7801\u4f8b\u5b50\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3 rand \u548c srand \u51fd\u6570\u5728\u7a0b\u5e8f\u4e2d\u7684\u8fd0\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 C \u8bed\u8a00\u5b9e\u73b0\u968f\u673a\u6570\u751f\u6210\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>#include &lt;stdlib.h&gt;<\/p>\n<p>#include &lt;time.h&gt;<\/p>\n<p>int m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    \/\/ \u521d\u59cb\u5316\u968f\u673a\u6570\u751f\u6210\u5668<\/p>\n<p>    srand((unsigned) time(NULL));<\/p>\n<p>    \/\/ \u751f\u6210\u5e76\u6253\u5370\u968f\u673a\u6570<\/p>\n<p>    for(int i = 0; i &lt; 10; i++) {<\/p>\n<p>        printf(&quot;%d\\n&quot;, rand());<\/p>\n<p>    }<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ed3\u679c\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u4ee3\u7801\u9996\u5148\u8c03\u7528 srand \u51fd\u6570\uff0c\u4f7f\u7528\u5f53\u524d\u65f6\u95f4\u4f5c\u4e3a\u79cd\u5b50\u503c\uff0c\u7136\u540e\u6253\u5370\u51fa10\u4e2a\u968f\u673a\u6570\u3002\u7531\u4e8e\u4f7f\u7528\u4e86\u52a8\u6001\u79cd\u5b50\u503c\uff08\u5373\u5f53\u524d\u65f6\u95f4\uff09\uff0c\u6bcf\u6b21\u6267\u884c\u7a0b\u5e8f\u65f6\u6253\u5370\u51fa\u7684\u968f\u673a\u6570\u5e8f\u5217\u90fd\u4f1a\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u968f\u673a\u6570\u751f\u6210\u7684\u5c40\u9650\u6027\u548c\u5982\u4f55\u6539\u5584<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1 rand \u548c srand \u7684\u7ed3\u5408\u80fd\u63d0\u4f9b\u57fa\u7840\u7684\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\uff0c\u4f46\u5b83\u4eec\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002<\/p>\n<\/p>\n<p><h4>PRNG\u7684\u5c40\u9650\u6027<\/h4>\n<\/p>\n<p><p>PRNG\u7684\u968f\u673a\u6027\u662f\u57fa\u4e8e\u6570\u5b66\u7b97\u6cd5\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u53ef\u4ee5\u88ab\u9884\u6d4b\uff0c\u8fd9\u5728\u9700\u8981\u9ad8\u7ea7\u5b89\u5168\u7684\u9886\u57df\uff0c\u6bd4\u5982\u52a0\u5bc6\uff0c\u53ef\u80fd\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002<\/p>\n<\/p>\n<p><h4>\u771f\u968f\u673a\u6570\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5b89\u5168\u6027\u8f83\u9ad8\u7684\u5e94\u7528\uff0c\u53ef\u8003\u8651\u4f7f\u7528\u57fa\u4e8e\u786c\u4ef6\u7684\u771f\u968f\u673a\u6570\u751f\u6210\u5668\uff08TRNG\uff09\u3002\u5b83\u4eec\u4ece\u7269\u7406\u73b0\u8c61\u4e2d\u63d0\u53d6\u968f\u673a\u6027\uff0c\u5982\u7535\u5b50\u566a\u58f0\uff0c\u65e0\u6cd5\u88ab\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>rand \u548c srand \u662f\u751f\u6210\u968f\u673a\u6570\u7684\u57fa\u7840\u5de5\u5177\u3002\u4f7f\u7528 srand \u8bbe\u7f6e\u4e0d\u540c\u7684\u79cd\u5b50\u53ef\u4ee5\u63d0\u5347 rand \u51fd\u6570\u751f\u6210\u968f\u673a\u6570\u5e8f\u5217\u7684\u968f\u673a\u6027\u3002\u4e86\u89e3\u5b83\u4eec\u7684\u5c40\u9650\u6027\u5e76\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u8003\u8651\u662f\u5426\u9700\u8981\u771f\u968f\u673a\u6570\u751f\u6210\u5668\uff0c\u662f\u7f16\u7a0b\u65f6\u9700\u8981\u8003\u8651\u7684\u91cd\u8981\u56e0\u7d20\u3002\u5728\u8bb8\u591a\u975e\u5b89\u5168\u6027\u5173\u952e\u7684\u573a\u5408\uff0c\u901a\u8fc7\u6b63\u786e\u4f7f\u7528 rand \u548c srand \u5c31\u8db3\u4ee5\u6ee1\u8db3\u968f\u673a\u6570\u751f\u6210\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e3a\u4ec0\u4e48\u5728C\/C++\u4e2d\u8981\u4f7f\u7528srand\u51fd\u6570\u6765\u751f\u6210\u968f\u673a\u6570\u79cd\u5b50\uff0c\u800c\u4e0d\u662frand\u51fd\u6570\u672c\u8eab\uff1f<\/strong><\/p>\n<p>\u5728C\/C++\u4e2d\uff0crand\u51fd\u6570\u7528\u4e8e\u751f\u6210\u4f2a\u968f\u673a\u6570\uff0c\u4f46\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0crand\u51fd\u6570\u751f\u6210\u7684\u5e8f\u5217\u90fd\u662f\u76f8\u540c\u7684\u3002\u4e3a\u4e86\u83b7\u5f97\u4e0d\u540c\u7684\u968f\u673a\u6570\u5e8f\u5217\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528srand\u51fd\u6570\u6765\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50\u3002srand\u51fd\u6570\u4ee5\u4e00\u4e2a\u6574\u6570\u503c\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u6574\u6570\u503c\u53ef\u4ee5\u7528\u6765\u521d\u59cb\u5316\u968f\u673a\u6570\u751f\u6210\u5668\u7684\u8d77\u59cb\u72b6\u6001\uff0c\u4ece\u800c\u4ea7\u751f\u4e0d\u540c\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528rand\u548csrand\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\uff1f<\/strong><\/p>\n<p>\u8981\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u516c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-c++\">int randomNum = (rand() % (maxNum - minNum + 1)) + minNum;\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff0cmaxNum\u662f\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6700\u5927\u503c\uff0cminNum\u662f\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6700\u5c0f\u503c\u3002rand() % (maxNum &#8211; minNum + 1)\u4f1a\u751f\u6210\u4e00\u4e2a\u57280\u5230(maxNum &#8211; minNum)\u4e4b\u95f4\u7684\u968f\u673a\u6570\uff0c\u7136\u540e\u518d\u52a0\u4e0aminNum\uff0c\u5373\u53ef\u5f97\u5230\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\u3002<\/p>\n<p><strong>3. srand\u51fd\u6570\u7684\u53c2\u6570\u5e94\u8be5\u8bbe\u7f6e\u4e3a\u4ec0\u4e48\u503c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u968f\u673a\u6027\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u968f\u673a\u6027\uff0csrand\u51fd\u6570\u7684\u53c2\u6570\u5e94\u8be5\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u968f\u673a\u53d8\u91cf\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528time\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u968f\u673a\u6570\u79cd\u5b50\u3002time\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u7684\u7cfb\u7edf\u65f6\u95f4\uff0c\u56e0\u6b64\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u4e0d\u540c\u7684\u968f\u673a\u6570\u79cd\u5b50\uff0c\u4ece\u800c\u751f\u6210\u4e0d\u540c\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-c++\">srand(time(0));\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u4e0d\u540c\u7684\u968f\u673a\u6570\u79cd\u5b50\uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u597d\u7684\u968f\u673a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"rand \u51fd\u6570\u662f\u7528\u4e8e\u751f\u6210\u4f2a\u968f\u673a\u6570\u7684\u3001\u800c srand \u662f\u4e00\u4e2a\u7528\u6765\u8bbe\u7f6e rand \u51fd\u6570\u4ea7\u751f\u968f\u673a\u6570\u5e8f\u5217\u79cd\u5b50\u7684\u51fd\u6570\u3002\u8fd9 [&hellip;]","protected":false},"author":3,"featured_media":316418,"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\/316376"}],"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=316376"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/316376\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/316418"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=316376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=316376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=316376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}