Skip to content

Instantly share code, notes, and snippets.

@micheleb
Last active January 14, 2021 16:29
Show Gist options
  • Save micheleb/f635eccb7c51d3f46328ffbc6326e664 to your computer and use it in GitHub Desktop.
Save micheleb/f635eccb7c51d3f46328ffbc6326e664 to your computer and use it in GitHub Desktop.
Generate random IMEI numbers
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());
}
}
@gcdd1993
Copy link

miss import import static java.util.Locale.ENGLISH;

@micheleb
Copy link
Author

updated, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment