Generate random CPF in C#

Asked

Viewed 5,551 times

6

I need a generic class to generate valid CPF to send value to fields with the webdriver Selenium.

Someone has the code?

Thank you.

  • 2

    You can try changing a CPF validator algorithm to generate Cpfs.

  • 2

    What is the difficulty/doubt exactly?

  • This is not a website for people to create code for you. Try to do the code, and if you get any error, post here the areas of the code related to the error, as well as the error occurring and the expected result.

  • A brief explanation of how to generate and validate valid CPF as well: www.geradorcpfvalido.com.br/formula-to-generate-Cpf.

2 answers

11


Follows an algorithm I built to generate a valid CPF in C#:

public static class CpfUtils
{
    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;
    }
}
  • 2

    Thank you, that’s exactly what I needed. Solved my problem.

1

Just complementing the already accepted answer, there is one more check that must be done to generate valid cpfs. Cpf cannot be composed of all the same digits, such as 111.111.111-11, 222.222.222-22 and so on and so forth.

We can also padde the string, to ensure the length of 9 characters without limiting the range of the random number.

public string GerarCpf()
{

    var random = new Random();

    int soma = 0;
    int resto = 0;
    int[] multiplicadores = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };
    string semente;

    do
    {
        semente = random.Next(1, 999999999).ToString().PadLeft(9, '0');
    }
    while (
        semente == "000000000"
        || semente == "111111111"
        || semente == "222222222"
        || semente == "333333333"
        || semente == "444444444"
        || semente == "555555555"
        || semente == "666666666"
        || semente == "777777777"
        || semente == "888888888"
        || semente == "999999999"
    );

    for (int i = 1; i < multiplicadores.Count(); i++)
        soma += int.Parse(semente[i - 1].ToString()) * multiplicadores[i];

    resto = soma % 11;

    if (resto < 2)
        resto = 0;
    else
        resto = 11 - resto;

    semente += resto;
    soma = 0;

    for (int i = 0; i < multiplicadores.Count(); i++)
        soma += int.Parse(semente[i].ToString()) * multiplicadores[i];

    resto = soma % 11;

    if (resto < 2)
        resto = 0;
    else
        resto = 11 - resto;

    semente = semente + resto;

    return semente;

}

We can also break the logic of the digit checker since we practically run it 2x. I have not tested the code below but if it does not work it is something very similar ;)

public string GerarCpf()
{

    var random = new Random();
    string semente;

    do
    {
        semente = random.Next(1, 999999999).ToString().PadLeft(9, '0');
    }
    while (
        semente == "000000000"
        || semente == "111111111"
        || semente == "222222222"
        || semente == "333333333"
        || semente == "444444444"
        || semente == "555555555"
        || semente == "666666666"
        || semente == "777777777"
        || semente == "888888888"
        || semente == "999999999"
    );

    semente += CalcularDigitoVerificador(semente).ToString();
    semente += CalcularDigitoVerificador(semente).ToString();
    return semente;

}

public int CalcularDigitoVerificador(string semente)
{
    int soma = 0;
    int resto = 0;
    int[] multiplicadores = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };

    var iFinal = multiplicadores.Count();
    var iInicial = iFinal - semente.Count;

    for (int i = iInicial; i < iFinal; i++)
        soma += int.Parse(semente[i - iInicial].ToString()) * multiplicadores[i];

    resto = soma % 11;

    if (resto < 2)
        resto = 0;
    else
        resto = 11 - resto;

    return resto;

}

And finally, although your question is about c#, Selenium has Apis for several languages. I posted a very similar version of this algorithm (generates state registrations) in Javascript on github.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.