Last active
August 24, 2020 09:38
-
-
Save anish000kumar/5b471a4ef464c03687f4b7c80dc47f57 to your computer and use it in GitHub Desktop.
Using rejection sampling to generate random number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The rand7() API is already defined for you. | |
# def rand7(): | |
# @return a random integer in the range 1 to 7 | |
class Solution: | |
def generate(self): | |
row, col = rand7(), rand7() | |
return ((row-1) * 7 ) + col | |
def rand10(self): | |
res = self.generate() | |
while res > 40: res = self.generate() | |
return ((res - 1) % 10 + 1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment