Skip to content

Instantly share code, notes, and snippets.

@georgepaoli
Last active January 31, 2023 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgepaoli/16fd769352646a888ebb157cfe982ff6 to your computer and use it in GitHub Desktop.
Save georgepaoli/16fd769352646a888ebb157cfe982ff6 to your computer and use it in GitHub Desktop.
Gerador de CPF em C#
// 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