Generate IMEI in python
Categories:
Generating Valid IMEI Numbers in Python

Learn how to programmatically generate valid International Mobile Equipment Identity (IMEI) numbers using Python, incorporating the Luhn algorithm for checksum validation.
The International Mobile Equipment Identity (IMEI) is a unique 15-digit number used to identify individual mobile phones. It's crucial for network operators to track devices and for security purposes, such as blocking stolen phones. While you generally won't need to generate real, active IMEIs, understanding their structure and validation is valuable for testing, simulation, or educational purposes. This article will guide you through generating a valid IMEI number in Python, focusing on the structure and the essential Luhn algorithm for checksum verification.
Understanding IMEI Structure
An IMEI number is composed of several parts, each providing specific information about the device. While the exact allocation of digits can vary slightly by standard, the general structure is as follows:
- TAC (Type Allocation Code): The first 8 digits. These identify the manufacturer and model of the device. The first two digits of the TAC are the Reporting Body Identifier (RBI).
- Serial Number (SNR): The next 6 digits. This is a unique serial number for the specific device model.
- Check Digit: The final (15th) digit. This is a checksum calculated using the Luhn algorithm, ensuring the validity of the entire IMEI.
flowchart LR A["IMEI (15 Digits)"] --> B["TAC (8 Digits)"] A --> C["Serial Number (6 Digits)"] A --> D["Check Digit (1 Digit)"] B --> B1["RBI (2 Digits)"] B --> B2["Manufacturer/Model (6 Digits)"] D["Check Digit (1 Digit)"] -- "Luhn Algorithm" --> E["Validation"]
Structure of an IMEI Number
The Luhn Algorithm for IMEI Validation
The Luhn algorithm, also known as the 'mod 10' algorithm, is a simple checksum formula used to validate a variety of identification numbers, including credit card numbers and IMEI numbers. It's designed to protect against accidental errors, such as single-digit transcription errors or transpositions of adjacent digits.
The algorithm works by performing a series of arithmetic operations on the digits of the number and then checking if the final sum is a multiple of 10. For IMEI, the 15th digit is specifically chosen to make the entire 15-digit number pass the Luhn check.
def calculate_luhn_checksum(number_str):
digits = [int(d) for d in number_str]
total_sum = 0
# Iterate from right to left, skipping the last digit (which would be the check digit)
# For IMEI, we're calculating the check digit, so we process 14 digits.
# If validating an existing 15-digit IMEI, you'd process all 15 and expect the sum % 10 == 0.
# For generating, we process the first 14 digits
for i, digit in enumerate(reversed(digits)):
if i % 2 == 0: # Every second digit from the right (0-indexed)
doubled_digit = digit * 2
if doubled_digit > 9:
total_sum += (doubled_digit - 9)
else:
total_sum += doubled_digit
else: # Other digits
total_sum += digit
return (10 - (total_sum % 10)) % 10
def is_valid_luhn(number_str):
digits = [int(d) for d in number_str]
total_sum = 0
for i, digit in enumerate(reversed(digits)):
if i % 2 == 1: # Every second digit from the right (0-indexed), starting with the second-to-last
doubled_digit = digit * 2
if doubled_digit > 9:
total_sum += (doubled_digit - 9)
else:
total_sum += doubled_digit
else: # Other digits
total_sum += digit
return total_sum % 10 == 0
# Example usage for validation
# print(is_valid_luhn("499012345678903")) # Example valid IMEI
# print(is_valid_luhn("499012345678904")) # Example invalid IMEI
Python implementation of the Luhn algorithm for checksum calculation and validation.
Generating a Full IMEI Number
To generate a complete 15-digit IMEI, we'll combine a randomly generated TAC, a random serial number, and then calculate the final check digit using the Luhn algorithm. For demonstration purposes, we'll use a placeholder TAC. In a real-world scenario, TACs are assigned and not randomly generated.
import random
def generate_imei():
# Step 1: Generate a random 8-digit TAC (Type Allocation Code)
# In a real scenario, TACs are assigned, not random.
# We'll use a common starting point for demonstration.
# Example TAC prefix for a generic device
tac_prefix = "356789"
remaining_tac_digits = ''.join([str(random.randint(0, 9)) for _ in range(8 - len(tac_prefix))])
tac = tac_prefix + remaining_tac_digits
# Step 2: Generate a random 6-digit Serial Number (SNR)
snr = ''.join([str(random.randint(0, 9)) for _ in range(6)])
# Combine TAC and SNR to form the first 14 digits
imei_14_digits = tac + snr
# Step 3: Calculate the Luhn check digit for the first 14 digits
# The calculate_luhn_checksum function from above is used here.
# Note: The `calculate_luhn_checksum` function needs to be defined or imported.
# For this example, we'll include it directly.
def calculate_luhn_checksum_for_generation(number_str):
digits = [int(d) for d in number_str]
total_sum = 0
# Iterate from right to left, processing 14 digits
for i, digit in enumerate(reversed(digits)):
if i % 2 == 0: # Every second digit from the right (0-indexed)
doubled_digit = digit * 2
if doubled_digit > 9:
total_sum += (doubled_digit - 9)
else:
total_sum += doubled_digit
else: # Other digits
total_sum += digit
return (10 - (total_sum % 10)) % 10
check_digit = calculate_luhn_checksum_for_generation(imei_14_digits)
# Step 4: Combine all parts to form the complete 15-digit IMEI
full_imei = imei_14_digits + str(check_digit)
return full_imei
# Generate and print a few IMEIs
for _ in range(3):
imei = generate_imei()
print(f"Generated IMEI: {imei}")
# Optionally, validate it
# print(f"Is valid (Luhn): {is_valid_luhn(imei)}") # Requires is_valid_luhn function
Python function to generate a random, valid 15-digit IMEI number.