Skip to content

Commit c5721c1

Browse files
authored
Create implement-rand10-using-rand7.py
1 parent 16fa698 commit c5721c1

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

implement-rand10-using-rand7.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution(SolBase):
2+
def rand10(self):
3+
# 推导过程可以参考 https://www.jianshu.com/p/1127e746da12
4+
while True:
5+
i = (self.rand7() - 1) * 7 + (self.rand7() - 1)
6+
if (i >= 1) and (i <= 10): # 因为i在1-10之间均匀分布,超出的值不要!
7+
return i
8+
9+
# medium: https://www.lintcode.com/problem/1496

0 commit comments

Comments
 (0)