Leetcode 903 Solution

This article provides solution to leetcode question 903 (implement-rand10-using-rand7)

https://leetcode.com/problems/implement-rand10-using-rand7

Solution

# The rand7() API is already defined for you.
# def rand7():
# @return a random integer in the range 1 to 7

class Solution:
    def rand10(self):
        """
        :rtype: int
        """
        while True:
            row = rand7() - 1
            col = rand7() - 1

            val = col + row * 7
            if val < 40:
                return 1 + (val % 10)