-
Notifications
You must be signed in to change notification settings - Fork 161
/
implement-rand10-using-rand7.md
53 lines (38 loc) · 1.43 KB
/
implement-rand10-using-rand7.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<p>Given a function <code>rand7</code> which generates a uniform random integer in the range 1 to 7, write a function <code>rand10</code> which generates a uniform random integer in the range 1 to 10.</p>
<p>Do NOT use system's <code>Math.random()</code>.</p>
<ol>
</ol>
<p> </p>
<div>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong><span id="example-input-1-1">1</span>
<strong>Output: </strong><span id="example-output-1">[7]</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong><span id="example-input-2-1">2</span>
<strong>Output: </strong><span id="example-output-2">[8,4]</span>
</pre>
<div>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input: </strong><span id="example-input-3-1">3</span>
<strong>Output: </strong><span id="example-output-3">[8,1,10]</span>
</pre>
<p> </p>
<p><strong>Note:</strong></p>
<ol>
<li><code>rand7</code> is predefined.</li>
<li>Each testcase has one argument: <code>n</code>, the number of times that <code>rand10</code> is called.</li>
</ol>
<p> </p>
<p><strong>Follow up:</strong></p>
<ol>
<li>What is the <a href="https://en.wikipedia.org/wiki/Expected_value" target="_blank">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>
</ol>
</div>
</div>
</div>