Last active
January 31, 2023 16:15
-
-
Save georgepaoli/16fd769352646a888ebb157cfe982ff6 to your computer and use it in GitHub Desktop.
Gerador de CPF em C#
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
// Fonte: https://pt.stackoverflow.com/a/33685/26670 | |
public static String GerarCpf() | |
{ | |
int soma = 0, resto = 0; | |
int[] multiplicador1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3, 2 }; | |
int[] multiplicador2 = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 }; | |
Random rnd = new Random(); | |
string semente = rnd.Next(100000000, 999999999).ToString(); | |
for (int i = 0; i < 9; i++) | |
soma += int.Parse(semente[i].ToString()) * multiplicador1[i]; | |
resto = soma % 11; | |
if (resto < 2) | |
resto = 0; | |
else | |
resto = 11 - resto; | |
semente = semente + resto; | |
soma = 0; | |
for (int i = 0; i < 10; i++) | |
soma += int.Parse(semente[i].ToString()) * multiplicador2[i]; | |
resto = soma % 11; | |
if (resto < 2) | |
resto = 0; | |
else | |
resto = 11 - resto; | |
semente = semente + resto; | |
return semente; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment