Generate Random IMEI Numbers in Java | Code Example

Learn how to generate random IMEI (International Mobile Equipment Identity) numbers in Java using a simple algorithm. Follow this code example to create valid IMEI numbers for testing or educational purposes.

RandomIMEI.java

import java.util.Random;
import static java.lang.Math.abs;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;

public class RandomIMEI {

    private static final Random GENERATOR = new Random();
    
    private static final String[] IMEI_REPORTING_BODY_IDS = {"01", "10", "30", "33", "35", "44",
            "45", "49", "50", "51", "52", "53", "54", "86", "91", "98", "99"};

    private static int sumDigits(int number) {
        int a = 0;
        while (number > 0) {
            a = a + number % 10;
            number = number / 10;
        }
        return a;
    }

    private static String generateImei() {
        String first14 = format("%s%.12s", 
                IMEI_REPORTING_BODY_IDS[GENERATOR.nextInt(IMEI_REPORTING_BODY_IDS.length)],
                format(ENGLISH, "%012d", abs(GENERATOR.nextLong())));

        int sum = 0;

        for (int i = 0; i < first14.length(); i++) {
            int c = Character.digit(first14.charAt(i), 10);
            sum += (i % 2 == 0 ? c : sumDigits(c * 2));
        }

        int finalDigit = (10 - (sum % 10)) % 10;

        return first14 + finalDigit;
    }

    public static void main(String[] args) {
        System.out.println(generateImei());
    }
}

In conclusion, the provided Java code generates random IMEI (International Mobile Equipment Identity) numbers adhering to the GSM standards. It employs a systematic approach to ensure that the generated IMEI numbers are valid, including a checksum digit for verification.

However, it’s essential to use such code responsibly and refrain from generating IMEI numbers for any unlawful or unethical purposes. IMEI numbers are unique identifiers crucial for tracking and authenticating mobile devices, and their misuse can lead to legal consequences.

Therefore, while this code serves educational purposes, it’s crucial to handle IMEI numbers with care and respect for privacy and security regulations.

Leave a Comment