java.lang.Object
g0401_0500.s0470_implement_rand10_using_rand7.Solution

public class Solution extends Object
470 - Implement Rand10() Using Rand7().<p>Medium</p> <p>Given the <strong>API</strong> <code>rand7()</code> that generates a uniform random integer in the range <code>[1, 7]</code>, write a function <code>rand10()</code> that generates a uniform random integer in the range <code>[1, 10]</code>. You can only call the API <code>rand7()</code>, and you shouldn&rsquo;t call any other API. Please <strong>do not</strong> use a language&rsquo;s built-in random API.</p> <p>Each test case will have one <strong>internal</strong> argument <code>n</code>, the number of times that your implemented function <code>rand10()</code> will be called while testing. Note that this is <strong>not an argument</strong> passed to <code>rand10()</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 1</p> <p><strong>Output:</strong> [2]</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 2</p> <p><strong>Output:</strong> [2,8]</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 3</p> <p><strong>Output:</strong> [3,8,10]</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 10<sup>5</sup></code></li> </ul> <p><strong>Follow up:</strong></p> <ul> <li>What is the <a href="https://en.wikipedia.org/wiki/Expected_value" target="_top">expected value</a> for the number of calls to <code>rand7()</code> function?</li> <li>Could you minimize the number of calls to <code>rand7()</code>?</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • rand10

      public int rand10()