Golang | Leetcode Golang题解之第470题用Rand7()实现Rand10()
题目:
题解:
func rand10() int {for {a := rand7()b := rand7()idx := (a-1)*7 + bif idx <= 40 {return 1 + (idx-1)%10}a = idx - 40b = rand7()// get uniform dist from 1 - 63idx = (a-1)*7 + bif idx <= 60 {return 1 + (idx-1)%10}a = idx - 60b = rand7()// get uniform dist from 1 - 21idx = (a-1)*7 + bif idx <= 20 {return 1 + (idx-1)%10}}
}