Generate valid, random CPF (Cadastro de Pessoas Físicas) numbers for testing purposes. This tool creates CPFs that comply with the official Brazilian format and validation rules, without using any real personal information.
The CPF (Cadastro de Pessoas Físicas) is a unique identifier assigned to Brazilian citizens and residents for tax purposes. This generator creates valid, random CPF numbers for testing purposes. It's important to note that these generated CPFs are not associated with real individuals and should only be used in testing environments.
A CPF consists of 11 digits:
The format is typically represented as: XXX.XXX.XXX-XX
The CPF validation algorithm ensures the integrity of the number. Here's how it works:
The formula for generating a valid CPF is as follows:
1def generate_cpf():
2 # Generate 9 random digits
3 base_cpf = [random.randint(0, 9) for _ in range(9)]
4
5 # Calculate first check digit
6 sum_1 = sum((10 - i) * digit for i, digit in enumerate(base_cpf))
7 check_digit_1 = (sum_1 * 10 % 11) % 10
8
9 # Calculate second check digit
10 sum_2 = sum((11 - i) * digit for i, digit in enumerate(base_cpf + [check_digit_1]))
11 check_digit_2 = (sum_2 * 10 % 11) % 10
12
13 return base_cpf + [check_digit_1, check_digit_2]
14
The generator performs the following steps:
Here are some examples of generated CPFs:
The CPF generator is useful in various software development and testing scenarios:
While the CPF is the primary individual identifier in Brazil, there are other identifiers that may be used in specific contexts:
The CPF was introduced in Brazil in 1965 as part of a broader effort to modernize the country's tax system. Initially, it was used primarily for income tax purposes, but over time, its use expanded to various other areas of Brazilian bureaucracy and commerce.
Key milestones in CPF history:
Today, the CPF is an essential part of Brazilian citizens' and residents' interactions with government services, financial institutions, and many commercial transactions.
Here are code snippets for generating and validating CPFs in various programming languages:
1import random
2
3def generate_cpf():
4 cpf = [random.randint(0, 9) for _ in range(9)]
5 for _ in range(2):
6 value = sum((cpf[num] * ((len(cpf) + 1) - num) for num in range(len(cpf)))) % 11
7 cpf.append(11 - value if value > 1 else 0)
8 return ''.join(map(str, cpf))
9
10def validate_cpf(cpf):
11 numbers = [int(digit) for digit in cpf if digit.isdigit()]
12 if len(numbers) != 11 or len(set(numbers)) == 1:
13 return False
14 for i in range(9, 11):
15 value = sum((numbers[num] * ((i + 1) - num) for num in range(i))) % 11
16 if numbers[i] != (11 - value if value > 1 else 0):
17 return False
18 return True
19
20## Example usage
21cpf = generate_cpf()
22print(f"Generated CPF: {cpf}")
23print(f"Is valid: {validate_cpf(cpf)}")
24
1function generateCPF() {
2 const cpf = Array.from({length: 9}, () => Math.floor(Math.random() * 10));
3 for (let i = 0; i < 2; i++) {
4 let sum = cpf.reduce((acc, cur, idx) => acc + cur * (cpf.length + 1 - idx), 0);
5 let digit = 11 - (sum % 11);
6 cpf.push(digit > 9 ? 0 : digit);
7 }
8 return cpf.join('');
9}
10
11function validateCPF(cpf) {
12 const numbers = cpf.match(/\d/g).map(Number);
13 if (numbers.length !== 11 || new Set(numbers).size === 1) return false;
14 for (let i = 9; i < 11; i++) {
15 let sum = numbers.slice(0, i).reduce((acc, cur, idx) => acc + cur * (i + 1 - idx), 0);
16 let digit = 11 - (sum % 11);
17 if (numbers[i] !== (digit > 9 ? 0 : digit)) return false;
18 }
19 return true;
20}
21
22// Example usage
23const cpf = generateCPF();
24console.log(`Generated CPF: ${cpf}`);
25console.log(`Is valid: ${validateCPF(cpf)}`);
26
1import java.util.Random;
2
3public class CPFGenerator {
4 private static final Random random = new Random();
5
6 public static String generateCPF() {
7 int[] cpf = new int[11];
8 for (int i = 0; i < 9; i++) {
9 cpf[i] = random.nextInt(10);
10 }
11 cpf[9] = calculateCheckDigit(cpf, 10);
12 cpf[10] = calculateCheckDigit(cpf, 11);
13 return formatCPF(cpf);
14 }
15
16 private static int calculateCheckDigit(int[] cpf, int factor) {
17 int sum = 0;
18 for (int i = 0; i < factor - 1; i++) {
19 sum += cpf[i] * (factor - i);
20 }
21 int result = 11 - (sum % 11);
22 return result > 9 ? 0 : result;
23 }
24
25 private static String formatCPF(int[] cpf) {
26 return String.format("%d%d%d.%d%d%d.%d%d%d-%d%d",
27 cpf[0], cpf[1], cpf[2], cpf[3], cpf[4], cpf[5], cpf[6], cpf[7], cpf[8], cpf[9], cpf[10]);
28 }
29
30 public static boolean validateCPF(String cpf) {
31 cpf = cpf.replaceAll("[^0-9]", "");
32 if (cpf.length() != 11) return false;
33 int[] numbers = cpf.chars().map(Character::getNumericValue).toArray();
34 if (allEqual(numbers)) return false;
35 int digit10 = calculateCheckDigit(numbers, 10);
36 int digit11 = calculateCheckDigit(numbers, 11);
37 return numbers[9] == digit10 && numbers[10] == digit11;
38 }
39
40 private static boolean allEqual(int[] array) {
41 for (int i = 1; i < array.length; i++) {
42 if (array[i] != array[0]) return false;
43 }
44 return true;
45 }
46
47 public static void main(String[] args) {
48 String cpf = generateCPF();
49 System.out.println("Generated CPF: " + cpf);
50 System.out.println("Is valid: " + validateCPF(cpf));
51 }
52}
53
These examples demonstrate how to generate and validate CPFs in Python, JavaScript, and Java. You can adapt these functions to your specific needs or integrate them into larger systems that require CPF handling.
When using generated CPFs, it's crucial to keep the following points in mind:
Discover more tools that might be useful for your workflow