Get Rand10 from Rand7
https://www.lintcode.com/problem/implement-rand10-using-rand7/description

class Solution(SolBase):
def rand10(self):
while True:
rand48 = self.rand7()-1 + (self.rand7()-1)*7
if rand48 <= 39:
return rand48 // 4 + 1
事实上,我们只用平凑近似于10的倍数的rand即可,所以我们弄出36 再取29 最后除以3也能得到类似的结果
class Solution(SolBase):
def rand10(self):
while True:
rand36 = self.rand7()-1 + (self.rand7()-1)*5
if rand48 <= 29:
return rand48 // 3 + 1
Last updated
Was this helpful?