{"id":120262,"date":"2026-10-31T09:48:58","date_gmt":"2026-10-31T01:48:58","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/120262.html"},"modified":"2026-10-31T09:48:58","modified_gmt":"2026-10-31T01:48:58","slug":"deepseek-leetcode-470-%e7%94%a8-rand7-%e5%ae%9e%e7%8e%b0-rand10-java%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/120262.html","title":{"rendered":"DeepSeek    LeetCode 470. \u7528 Rand7() \u5b9e\u73b0 Rand10() Java\u5b9e\u73b0"},"content":{"rendered":"<p>\u601d\u8def&#xff1a;\u62d2\u7edd\u91c7\u6837&#xff08;Rejection Sampling&#xff09;<\/p>\n<p>\u6838\u5fc3\u95ee\u9898&#xff1a;Rand7() \u751f\u6210 1~7 \u5747\u5300\u968f\u673a\u6570&#xff0c;\u76ee\u6807\u662f\u751f\u6210 1~10 \u5747\u5300\u968f\u673a\u6570\u30027 \u548c 10 \u6ca1\u6709\u500d\u6570\u5173\u7cfb&#xff0c;\u9700\u8981\u5148\u6269\u5927\u53d6\u503c\u8303\u56f4\u518d\u53d6\u6a21\u6620\u5c04\u3002<\/p>\n<p>\u57fa\u672c\u6784\u9020&#xff1a;\u4e24\u6b21\u8c03\u7528 Rand7() \u53ef\u4ee5\u751f\u6210 1~49 \u7684\u5747\u5300\u968f\u673a\u6570&#xff1a;<\/p>\n<p>x &#061; (rand7() &#8211; 1) * 7 &#043; rand7()<\/p>\n<p>(rand7()-1) \u751f\u6210 0~6 \u7684 7 \u8fdb\u5236\u9ad8\u4f4d&#xff0c;rand7() \u751f\u6210 1~7 \u7684\u4f4e\u4f4d&#xff0c;\u7ec4\u5408\u5f97\u5230 1~49&#xff0c;\u6bcf\u4e2a\u503c\u6982\u7387\u90fd\u662f 1\/49\u3002<\/p>\n<p>\u00b7 \u82e5 x &lt;&#061; 40&#xff0c;\u76f4\u63a5\u53d6\u6a21\u6620\u5c04&#xff1a;(x &#8211; 1) % 10 &#043; 1&#xff0c;\u80fd\u5f97\u5230\u5747\u5300\u7684 1~10&#xff08;\u56e0\u4e3a 40 \u662f 10 \u7684\u500d\u6570&#xff0c;\u6bcf\u7ec4 4 \u4e2a\u503c\u6070\u597d\u5bf9\u5e94\u4e00\u4e2a\u7ed3\u679c&#xff09;\u3002<br \/>\n\u00b7 \u82e5 x \u2208 [41, 49]&#xff0c;\u8bf4\u660e\u843d\u5728&#034;\u6d6a\u8d39\u533a\u95f4&#034;&#xff0c;\u91cd\u65b0\u91c7\u6837\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u6700\u7b80\u7248\u672c\u3002\u4f46 49 \u4e2a\u6570\u4e2d\u6d6a\u8d39\u4e86 9 \u4e2a&#xff0c;\u53ef\u4ee5\u628a\u6d6a\u8d39\u7684 9 \u4e2a\u6570\u7ee7\u7eed\u5229\u7528&#xff0c;\u8fdb\u4e00\u6b65\u964d\u4f4e\u671f\u671b\u8c03\u7528\u6b21\u6570\u3002<\/p>\n<hr \/>\n<p>Java \u5b9e\u73b0&#xff1a;\u4f18\u5316\u7248&#xff08;\u5229\u7528\u88ab\u62d2\u7edd\u7684\u4f59\u6570&#xff09;<\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * The rand7() API is already defined in the parent class SolBase.<br \/>\n * public int rand7();<br \/>\n * &#064;return a random integer in the range 1 to 7<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Solution<\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token class-name\">SolBase<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">int<\/span> <span class=\"token function\">rand10<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u7b2c\u4e00\u6b21&#xff1a;1 ~ 49<\/span><br \/>\n            <span class=\"token keyword\">int<\/span> x <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">rand7<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">7<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token function\">rand7<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>x <span class=\"token operator\">&lt;&#061;<\/span> <span class=\"token number\">40<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>x <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">%<\/span> <span class=\"token number\">10<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ x \u2208 [41, 49]&#xff0c;\u5171 9 \u4e2a\u6570&#xff0c;\u6620\u5c04\u5230 [1, 9]<\/span><br \/>\n            x <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>x <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">41<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">7<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token function\">rand7<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ 1 ~ 63<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>x <span class=\"token operator\">&lt;&#061;<\/span> <span class=\"token number\">60<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>x <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">%<\/span> <span class=\"token number\">10<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ x \u2208 [61, 63]&#xff0c;\u5171 3 \u4e2a\u6570&#xff0c;\u6620\u5c04\u5230 [1, 3]<\/span><br \/>\n            x <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>x <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">61<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">7<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token function\">rand7<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ 1 ~ 21<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>x <span class=\"token operator\">&lt;&#061;<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>x <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">%<\/span> <span class=\"token number\">10<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n            <span class=\"token comment\">\/\/ x &#061; 21 \u5219\u91cd\u65b0\u5faa\u73af<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4ee3\u7801\u8bf4\u660e<\/p>\n<p>\u00b7 \u7b2c\u4e00\u6b21\u91c7\u6837&#xff1a;x \u2208 [1, 49]&#xff0c;x &lt;&#061; 40 \u76f4\u63a5\u8fd4\u56de&#xff0c;\u5426\u5219\u8fdb\u5165\u7b2c\u4e8c\u6b21\u3002<br \/>\n\u00b7 \u7b2c\u4e8c\u6b21\u91c7\u6837&#xff1a;\u628a [41, 49] \u5e73\u79fb\u5230 [0, 8]&#xff0c;\u518d\u4e58 7 \u52a0 rand7()&#xff0c;\u5f97\u5230 [1, 63]\u3002x &lt;&#061; 60 \u76f4\u63a5\u8fd4\u56de&#xff0c;\u5426\u5219\u8fdb\u5165\u7b2c\u4e09\u6b21\u3002<br \/>\n\u00b7 \u7b2c\u4e09\u6b21\u91c7\u6837&#xff1a;\u628a [61, 63] \u5e73\u79fb\u5230 [0, 2]&#xff0c;\u518d\u4e58 7 \u52a0 rand7()&#xff0c;\u5f97\u5230 [1, 21]\u3002x &lt;&#061; 20 \u76f4\u63a5\u8fd4\u56de&#xff0c;\u5426\u5219\u53ea\u5269 x &#061; 21&#xff0c;\u91cd\u65b0\u5faa\u73af\u3002<\/p>\n<p>\u6ce8\u610f (x &#8211; 41) * 7 &#043; rand7() \u4e2d x &#8211; 41 \u7684\u8303\u56f4\u662f 0~8&#xff0c;(x-41)*7 \u8303\u56f4 0~56&#xff0c;\u52a0 rand7() \u540e\u4e3a 1~63&#xff0c;\u786e\u4fdd\u6bcf\u4e2a\u503c\u7b49\u6982\u7387\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48 (x &#8211; 1) % 10 &#043; 1 \u662f\u5747\u5300\u7684&#xff1f;<\/p>\n<p>\u4ee5\u7b2c\u4e00\u6b21\u4e3a\u4f8b&#xff0c;1~40 \u5171 40 \u4e2a\u6570&#xff0c;\u6bcf\u4e2a\u6570\u6982\u7387 1\/49\u3002\u6309 (x-1)%10&#043;1 \u5206\u7ec4&#xff1a;<\/p>\n<p>\u7ed3\u679c \u5bf9\u5e94 x<br \/>\n1 1, 11, 21, 31<br \/>\n2 2, 12, 22, 32<br \/>\n\u2026 \u2026<br \/>\n10 10, 20, 30, 40<\/p>\n<p>\u6bcf\u7ec4\u6070\u597d 4 \u4e2a\u6570&#xff0c;\u6982\u7387\u90fd\u662f 4\/49&#xff0c;\u56e0\u6b64\u5747\u5300\u3002<\/p>\n<p>\u671f\u671b\u8c03\u7528\u6b21\u6570\u5206\u6790<\/p>\n<p>\u9636\u6bb5 \u89e6\u53d1\u6982\u7387 \u6d88\u8017 rand7 \u6b21\u6570<br \/>\n\u7b2c\u4e00\u6b21\u6210\u529f 40\/49 2<br \/>\n\u7b2c\u4e8c\u6b21\u6210\u529f (9\/49) * (60\/63) 4<br \/>\n\u7b2c\u4e09\u6b21\u6210\u529f (9\/49) * (3\/63) * (20\/21) 6<br \/>\n\u5faa\u73af \u5269\u4f59\u6781\u5c0f \u2014<\/p>\n<p>\u671f\u671b\u8c03\u7528\u6b21\u6570 \u2248 2 \u00d7 (40\/49) &#043; 4 \u00d7 (9\/49)(60\/63) &#043; 6 \u00d7 (9\/49)(3\/63)(20\/21) &#043; \u2026 \u2248 2.376 \u6b21&#xff0c;\u4f18\u4e8e\u7b80\u5355\u7248\u672c\u7684\u7ea6 2.45 \u6b21\u3002<\/p>\n<p>\u590d\u6742\u5ea6<\/p>\n<p>\u00b7 \u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;\u671f\u671b O(1)&#xff0c;\u671f\u671b\u8c03\u7528 rand7() \u7ea6 2.376 \u6b21\u3002<br \/>\n\u00b7 \u7a7a\u95f4\u590d\u6742\u5ea6&#xff1a;O(1)\u3002<\/p>\n<p>\u6613\u9519\u70b9<\/p>\n<li>\u4e0d\u8981\u7528 rand7() % 10 \u4e4b\u7c7b\u7684\u6620\u5c04&#xff1a;7 \u548c 10 \u65e0\u500d\u6570\u5173\u7cfb&#xff0c;\u76f4\u63a5\u53d6\u6a21\u4f1a\u5bfc\u81f4\u5206\u5e03\u4e0d\u5747\u3002<\/li>\n<li>\u4e58\u6cd5\u7ec4\u5408\u8981\u7528 (rand7() &#8211; 1) * 7 &#043; rand7()&#xff0c;\u82e5\u5199\u6210 rand7() * 7 &#043; rand7() \u4f1a\u5f97\u5230 8~56 \u800c\u975e 1~49&#xff0c;\u4e14\u8fb9\u754c\u503c\u6982\u7387\u4e0d\u540c\u3002<\/li>\n<li>\u62d2\u7edd\u533a\u95f4\u5fc5\u987b\u91cd\u65b0\u91c7\u6837&#xff0c;\u4e0d\u80fd\u7b80\u5355 % 10&#xff0c;\u5426\u5219 41~49 \u4f1a\u504f\u5411 1~9\u3002<\/li>\n<li>\u4f18\u5316\u7248\u4e2d\u4f59\u6570\u5e73\u79fb\u8981\u5199\u5bf9&#xff1a;(x &#8211; 41) \u800c\u4e0d\u662f (x &#8211; 40)&#xff0c;\u56e0\u4e3a 41 \u8981\u6620\u5c04\u5230 0\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261031014856-6ae549080b4b1.jpg\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u8def&#xff1a;\u62d2\u7edd\u91c7\u6837&#xff08;Rejection Sampling&#xff09;<br \/>\n\u6838\u5fc3\u95ee\u9898&#xff1a;Rand7() \u751f\u6210 1~7 \u5747\u5300\u968f\u673a\u6570&#xff0c;\u76ee\u6807\u662f\u751f\u6210 1~10 \u5747\u5300\u968f\u673a\u6570\u30027 \u548c 10 \u6ca1\u6709\u500d\u6570\u5173\u7cfb&#xff0c;\u9700\u8981\u5148\u6269\u5927\u53d6\u503c\u8303\u56f4\u518d\u53d6\u6a21\u6620\u5c04\u3002<br \/>\n\u57fa\u672c\u6784\u9020&#xff1a;\u4e24\u6b21\u8c03\u7528 Rand7() \u53ef\u4ee5\u751f\u6210 1~49 \u7684\u5747\u5300\u968f\u673a\u6570&#xff1a;<br \/>\nx  (rand7() &#8211; 1) * 7  rand7()(rand7()-1) \u751f\u6210 0~6<\/p>\n","protected":false},"author":2,"featured_media":120261,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,2692,292,1813,427],"topic":[],"class_list":["post-120262","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java","tag-leetcode","tag-292","tag-1813","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>DeepSeek  LeetCode 470. \u7528 Rand7() \u5b9e\u73b0 Rand10() Java\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/120262.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"DeepSeek  LeetCode 470. \u7528 Rand7() \u5b9e\u73b0 Rand10() Java\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u601d\u8def&#xff1a;\u62d2\u7edd\u91c7\u6837&#xff08;Rejection Sampling&#xff09; \u6838\u5fc3\u95ee\u9898&#xff1a;Rand7() \u751f\u6210 1~7 \u5747\u5300\u968f\u673a\u6570&#xff0c;\u76ee\u6807\u662f\u751f\u6210 1~10 \u5747\u5300\u968f\u673a\u6570\u30027 \u548c 10 \u6ca1\u6709\u500d\u6570\u5173\u7cfb&#xff0c;\u9700\u8981\u5148\u6269\u5927\u53d6\u503c\u8303\u56f4\u518d\u53d6\u6a21\u6620\u5c04\u3002 \u57fa\u672c\u6784\u9020&#xff1a;\u4e24\u6b21\u8c03\u7528 Rand7() \u53ef\u4ee5\u751f\u6210 1~49 \u7684\u5747\u5300\u968f\u673a\u6570&#xff1a; x (rand7() - 1) * 7 rand7()(rand7()-1) \u751f\u6210 0~6\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/120262.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-31T01:48:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261031014856-6ae549080b4b1.jpg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/120262.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/120262.html\",\"name\":\"DeepSeek LeetCode 470. \u7528 Rand7() \u5b9e\u73b0 Rand10() Java\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-31T01:48:58+00:00\",\"dateModified\":\"2026-10-31T01:48:58+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/120262.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/120262.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/120262.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"DeepSeek LeetCode 470. \u7528 Rand7() \u5b9e\u73b0 Rand10() Java\u5b9e\u73b0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"DeepSeek  LeetCode 470. \u7528 Rand7() \u5b9e\u73b0 Rand10() Java\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/120262.html","og_locale":"zh_CN","og_type":"article","og_title":"DeepSeek  LeetCode 470. \u7528 Rand7() \u5b9e\u73b0 Rand10() Java\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u601d\u8def&#xff1a;\u62d2\u7edd\u91c7\u6837&#xff08;Rejection Sampling&#xff09; \u6838\u5fc3\u95ee\u9898&#xff1a;Rand7() \u751f\u6210 1~7 \u5747\u5300\u968f\u673a\u6570&#xff0c;\u76ee\u6807\u662f\u751f\u6210 1~10 \u5747\u5300\u968f\u673a\u6570\u30027 \u548c 10 \u6ca1\u6709\u500d\u6570\u5173\u7cfb&#xff0c;\u9700\u8981\u5148\u6269\u5927\u53d6\u503c\u8303\u56f4\u518d\u53d6\u6a21\u6620\u5c04\u3002 \u57fa\u672c\u6784\u9020&#xff1a;\u4e24\u6b21\u8c03\u7528 Rand7() \u53ef\u4ee5\u751f\u6210 1~49 \u7684\u5747\u5300\u968f\u673a\u6570&#xff1a; x (rand7() - 1) * 7 rand7()(rand7()-1) \u751f\u6210 0~6","og_url":"https:\/\/www.wsisp.com\/helps\/120262.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-31T01:48:58+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261031014856-6ae549080b4b1.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/120262.html","url":"https:\/\/www.wsisp.com\/helps\/120262.html","name":"DeepSeek LeetCode 470. \u7528 Rand7() \u5b9e\u73b0 Rand10() Java\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-31T01:48:58+00:00","dateModified":"2026-10-31T01:48:58+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/120262.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/120262.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/120262.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"DeepSeek LeetCode 470. \u7528 Rand7() \u5b9e\u73b0 Rand10() Java\u5b9e\u73b0"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/120262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=120262"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/120262\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/120261"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=120262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=120262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=120262"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=120262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}