How PHP generates non-repeating card numbers
It's like a phone card, all numbers, not letters.
The first thought was:
N-bit random number + self-increment id+n bit random number for a database table
Do not want to use the database, think of a unique standard is time. Use a timestamp to keep up with several random numbers.
If this generates 16 of the card number:
Timestamp 10 bit + 6 bit random number 1316651745695261
Sometimes it takes a loop to generate multiple card numbers, a timestamp in front of a loop (basically the same, unless there are other time-consuming operations in the loop), and the subsequent 6-bit random number is not guaranteed to be unique. Thinking of combining Microtime
Loop call Microtime 10 times:
0.80717200 1316675664
0.80720600 1316675664
0.80721300 1316675664
0.80721900 1316675664
0.80722400 1316675664
0.80723000 1316675664
0.80723600 1316675664
0.80724100 1316675664
0.80724600 1316675664
0.80725200 1316675664
I think it's keeping up with the number of positions in 807172 here to facilitate the separation. Code:
PHP Code
for ($i =0; $i <10; $i + +) { //echo $this->microtime_float (); echo $time = Microtime (); $arr = Explode ("", $time); $micro = $arr [0]*100000000; $micro _str = substr ($micro. "", 0,6); $timestamp = $arr [1]; echo "\ t". $timestamp. "". $micro _str; Echo '
'; }
For the time being the local Windows test will not be duplicated, will there be a comparison of the server computing power Bull X, microsecond also repeated?
In addition, this generated card number is not good at a glance can see what. and can not arbitrarily intercept several of them afraid of losing the uniqueness. I wonder if there is a good way to have wood.
such as mobile recharge card generation mechanism? A little bit less, too much, people are dizzy ...
------Solution--------------------
The number of microseconds is unlikely, and there are random numbers behind you, and if you're not sure you can add usleep (1) to wait.
But if the server modification time you can not guarantee that the number of microseconds is not repeated.
------Solution--------------------
If you use only timestamps (repeatable) + random numbers (repeatable), there is no guarantee that 100% will not conflict ...
Recommendation: Timestamps (possibly duplicates) + random numbers (possibly duplicates) + database IDs or other unique values (unique) to get non-repeatable results.