PHP generates unique number 36 binary non-repeating number
The PHP implementation generates a unique number, using the 10 binary conversion 36 binary to get the unique number of 6000多万个, the number of digits is 10 bits.
When a large number of data to be numbered, and the number has a number of digits limit, such as 5-bit license plate number, 10-bit ID number, order serial number, short URL, etc., we can use the 36 binary to calculate the number of non-duplicated digits.
We will 0-z (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) represent the value 0-35, such as the letter Z for 35. So I'm going to get a 5 digit number, the maximum amount of information is 36 of 5 square, 36^5 = 60466176, that is, the largest 5-bit number is equivalent to 10 decimal number: 60466176.
In order to do the demonstration, we assume that a club issued a group of 10-digit membership card number, the membership card number is 3 City number + 5-digit card number code + 2-bit check code composition. City number with the area code, such as 755 for Shenzhen, 5-digit card number is 36 of the card number of the system, the following two-bit check code is generated by a certain algorithm, the usefulness of the check code is to verify the legitimacy of the card number. In this case, the 10-bit card number generated by (www.jbxue.com) is equivalent to the maximum number of more than 60 million loyalty cards and is a unique card number that is not duplicated.
PHP implementation
Using PHP for binary conversion, 10 binary to 36 binary.
classCode {//Password DictionariesPrivate $dic=Array( 0=> ' 0 ', 1=> ' 1 ', 2=> ' 2 ', 3=> ' 3 ', 4=> ' 4 ', 5=> ' 5 ', 6=> ' 6 ', 7=> ' 7 ', 8=> ' 8 ', 9=> ' 9 ', 10=> ' A ' , 11=> ' B ', 12=> ' C ', 13=> ' D ', 14=> ' E ', 15=> ' F ', 16=> ' G ', 17=> ' H ', 18=> ' I ',19=> ' J ', 20=> ' K ' , 21=> ' L ', 22=> ' M ', 23=> ' N ', 24=> ' O ', 25=> ' P ', 26=> ' Q ', 27=> ' R ',28=> ' S ', 29=> ' T ', 30=> ' U ' , 31=> ' V ', 32=> ' W ', 33=> ' X ', 34=> ' Y ', 35=> ' Z ' ); Public functionEncodeid ($int,$format=8) { $dics=$this-dic;$dnum= 36;//binary number$arr=Array (); $loop=true; while($loop) { $arr[] =$dics[Bcmod($int,$dnum)]; $int=Bcdiv($int,$dnum, 0); if($int= = ' 0 ') { $loop=false; } } if(Count($arr) <$format) $arr=Array_pad($arr,$format,$dics[0]); return implode(‘‘,Array_reverse($arr)); } Public functionDecodeid ($ids) { $dics=$this-dic;$dnum= 36;//binary number//key value Exchange$dedic=Array_flip($dics); //go to 0$id=LTrim($ids,$dics[0]); //reversal$id=Strrev($id); $v= 0; for($i= 0,$j=strlen($id);$i<$j;$i++) { $v=Bcadd(Bcmul($dedic[$id { $i } ],Bcpow($dnum,$i, 0), 0),$v, 0); } return $v; } }
Define the code class, first define the password dictionary, that is, the value corresponding to 0-z, method Encodeid ($int, $format) parameter $int representation of the number, $format the length of the number of bits, for example Encodeid (123456789,5) Represents a 36-digit number that converts the number 123456789 to a 5-bit, and the method Decodeid ($ids) is used to convert the 36-decimal number into a 10-binary number.
You can generate the card number by doing this:
$code New $card _no$code
As above, you can get a 5-bit card number, which actually represents the card number is 888888 (6 8) of the membership number, and the actual conversion is 5 digit number: 0J1VC.
Next, we add the city number and check code, the city number is already defined, the check code is obtained by certain algorithm, in this example, the use of simple algorithm:
The first three city number and five-digit card number for MD5 encryption, and then take the MD5 value of the first 2 bits as a checksum, so that the number behind the two-digit check code.
$card _pre = ' 755 '$card _vcsubstr(MD5($card _pre. $card _no), 0,2$card _vcstrtoupper($card _vcEcho $card _pre. $card _no. $card _VC
In practice, you can get 10 decimal numbers from the database, guarantee the unique number, and then combine the above code, resulting in a 10-bit non-repeating membership card number.