Created
March 10, 2023 10:59
-
-
Save tarikmanoar/257c0820413fa59cb05cf8c5965f4416 to your computer and use it in GitHub Desktop.
you can generate a 10-digit unique number that never duplicates using various techniques. One common approach is to use a combination of random numbers and a checksum digit. Here's an example implementation in PHP:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generate_unique_number() { | |
$number = mt_rand(1000000000, 9999999999); // Generate a random 10-digit number | |
$sum = 0; | |
for ($i = 2; $i <= 11; $i++) { // Calculate the checksum digit | |
$digit = substr($number, 11 - $i, 1); | |
$sum += $digit * $i; | |
} | |
$checksum = ($sum % 11) == 10 ? '0' : ($sum % 11); // If the checksum is 10, replace it with 0 | |
return substr($number, 0, 9) . $checksum; // Return the number with the checksum digit appended | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment