1 parent 791b90c commit 40ad26aCopy full SHA for 40ad26a
Implement Rand10() Using Rand7()/Implement_Rand10()_Using_Rand7().cpp
@@ -0,0 +1,21 @@
1
+// The rand7() API is already defined for you.
2
+// int rand7();
3
+// @return a random integer in the range 1 to 7
4
+
5
+class Solution
6
+{
7
+public:
8
+ int rand10()
9
+ {
10
+ while (true)
11
12
+ int bit1 = rand7();
13
+ int bit2 = rand7();
14
15
+ int res = (bit1 - 1) * 7 + bit2;
16
17
+ if (1 <= res && res <= 40)
18
+ return res % 10 + 1;
19
+ }
20
21
+};
0 commit comments