{"id":4192,"date":"2018-10-15T19:35:54","date_gmt":"2018-10-16T02:35:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4192"},"modified":"2018-10-19T22:03:42","modified_gmt":"2018-10-20T05:03:42","slug":"leetcode-470-implement-rand10-using-rand7","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-470-implement-rand10-using-rand7\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 470. Implement Rand10() Using Rand7()"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 470. Implement Rand10() Using Rand7() - \u5237\u9898\u627e\u5de5\u4f5c EP226\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/Wyauxe92JJA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>Given a function\u00a0<code>rand7<\/code>\u00a0which generates a uniform random integer in the range 1 to 7, write a function\u00a0<code>rand10<\/code>\u00a0which generates a uniform random integer in the range 1 to 10.<\/p>\n<p>Do NOT use system&#8217;s\u00a0<code>Math.random()<\/code>.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">1<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[7]<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">2<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[8,4]<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">3<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">[8,1,10]<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>rand7<\/code>\u00a0is predefined.<\/li>\n<li>Each testcase has one argument:\u00a0<code>n<\/code>, the number of times that\u00a0<code>rand10<\/code>\u00a0is called.<\/li>\n<\/ol>\n<p><strong>Follow up:<\/strong><\/p>\n<ol>\n<li>What is the\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Expected_value\" target=\"_blank\" rel=\"noopener\">expected value<\/a>\u00a0for the number of calls to\u00a0<code>rand7()<\/code>\u00a0function?<\/li>\n<li>Could you minimize the number of calls to\u00a0<code>rand7()<\/code>?<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4197\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/470-ep226-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/470-ep226-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/470-ep226-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/470-ep226-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4196\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/470-ep226-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/470-ep226-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/470-ep226-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/470-ep226-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1><strong>Solution:<\/strong><\/h1>\n<p>Time complexity: O(1)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int rand10() {\r\n    int index = INT_MAX;\r\n    while (index &gt;= 40)\r\n      index = 7 * (rand7() - 1) + (rand7() - 1);\r\n    return index % 10 + 1;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a function\u00a0rand7\u00a0which generates a uniform random integer in the range 1 to 7, write a function\u00a0rand10\u00a0which generates a uniform random integer in the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177,120],"class_list":["post-4192","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","tag-probability","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4192"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4192\/revisions"}],"predecessor-version":[{"id":4198,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4192\/revisions\/4198"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}