Generate random CPF in C #

5

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

Does anyone have the code?

Thank you.

    
asked by anonymous 24.09.2014 / 02:45

2 answers

9

Here is an algorithm I put together 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;
    }
}
    
24.09.2014 / 06:01
0

Just completing the already accepted response, there is one more check that must be made to generate valid cpfs. The cpf can not be composed of all equal digits like 111.111.111-11, 222.222.222-22 and so on.

We can also do a padding in the string, to guarantee 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 check digit since we're practically going to run it 2x. I did not test the code below but if it does not work, it's a good thing;)

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 lastly, although your question is about C #, selenium has APIs for various languages. I posted a pretty similar version of this algorithm (generates state inscriptions) in JavaScript on github .

    
16.11.2017 / 03:12