Skip to content

Instantly share code, notes, and snippets.

@acfreitas
Last active August 29, 2015 14:10
Show Gist options
  • Save acfreitas/63d9b2b103720a0084d9 to your computer and use it in GitHub Desktop.
Save acfreitas/63d9b2b103720a0084d9 to your computer and use it in GitHub Desktop.
Generator of aleatory IMEI code
/**
* Generates IMEI code valid and random
* Generates 14 aleatory digits. These 14, multiplies the multiples of 2 by 2 and sum the result
* The result Must be divisible by 10,
* Then get the diference and genaretes the last digit
*
* @return int $imei
*/
public function imeiRandom() {
$code = $this->intRandom(14);
$position = 0;
$total = 0;
while ($position < 14) {
if ($position % 2 == 0) {
$prod = 1;
} else {
$prod = 2;
}
$actualNum = $prod * $code[$position];
if ($actualNum > 9) {
$strNum = strval($actualNum);
$total += $strNum[0] + $strNum[1];
} else {
$total += $actualNum;
}
$position++;
}
$last = 10 - ($total % 10);
if ($last == 10) {
$imei = $code . 0;
} else {
$imei = $code . $last;
}
return $imei;
}
/**
* @param int $size
* @return $int
*/
public function intRandom($size) {
$validCharacters = utf8_decode("0123456789");
$validCharNumber = strlen($validCharacters);
$int = '';
while (strlen($int) < $size) {
$index = mt_rand(0, $validCharNumber - 1);
$int .= $validCharacters[$index];
}
return $int;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment