Last active
August 29, 2015 14:08
-
-
Save mrbitsdcf/508890b9387078f9b84d to your computer and use it in GitHub Desktop.
Gerador de CPF
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
import random | |
def gera_cpf_valido(mask=False): | |
n = [random.randrange(10) for i in xrange(9)] | |
# calcula digito 1 e acrescenta ao numero | |
s = sum(x * y for x, y in zip(n, range(10, 1, -1))) | |
d1 = 11 - s % 11 | |
if d1 >= 10: | |
d1 = 0 | |
n.append(d1) | |
# calcula digito 2 e acrescenta ao numero | |
s = sum(x * y for x, y in zip(n, range(11, 1, -1))) | |
d2 = 11 - s % 11 | |
if d2 >= 10: | |
d2 = 0 | |
n.append(d2) | |
if mask: | |
return "%d%d%d.%d%d%d.%d%d%d-%d%d" % tuple(n) | |
return "%d%d%d%d%d%d%d%d%d%d%d" % tuple(n) | |
if __name__ == '__main__': | |
gera_cpf_valido() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment