Skip to content

Instantly share code, notes, and snippets.

@anish000kumar
Last active August 24, 2020 09:38
Show Gist options
  • Save anish000kumar/5b471a4ef464c03687f4b7c80dc47f57 to your computer and use it in GitHub Desktop.
Save anish000kumar/5b471a4ef464c03687f4b7c80dc47f57 to your computer and use it in GitHub Desktop.
Using rejection sampling to generate random number
# 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