This article mainly introduces how to use PHP to generate non-repeating random numbers based on auto-increment data. The article gives detailed sample codes for your reference and study. It has certain reference value for everyone. Friends who need it Let’s take a look together below.
This article mainly introduces the relevant content of PHP based on self-increasing data to generate non-repeating random numbers. It is shared for everyone's reference and learning. It is useless to say more below and go directly to the code:
Key words The point is that the number of generated auto-increment data bits is controlled in two places
function swap($n,$base) { $mask = 19; //1 + 2 + 16 $n = intval($n, 10); $idx = $n & $mask;//取出标志位 $xor = $base[$idx] ^ $n;//根据标识为找到对应的base,异或n return (($xor | $mask) ^ $mask) | $idx;//覆盖标识位 } $newArray=[]; $base = array ( 0 => 1918407, 1 => 5529570, 2 => 6527234, 3 => 6550962, 4 => 8054672, 5 => 3141234, 6 => 2125630, 7 => 6978033, 8 => 4623630, 9 => 4736420, 10 => 7767295, 11 => 9679880, 12 => 1665600, 13 => 6044373, 14 => 4661383, 15 => 5771843, 16 => 2206123, 17 => 5685396, 18 => 5571647, 19 => 7178311, ); for ($i=0;$i<1000000;$i++) { $newArray[]= swap($i,$base)."\r\n"; }
The above is the detailed content of The random numbers generated when PHP data is automatically incremented are not repeated.. For more information, please follow other related articles on the PHP Chinese website!