Home > Backend Development > PHP Tutorial > The random numbers generated when PHP data is automatically incremented are not repeated.

The random numbers generated when PHP data is automatically incremented are not repeated.

巴扎黑
Release: 2023-03-15 14:00:01
Original
1791 people have browsed it

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

## 1. If the $base base array is an 8-bit array It must be 8 digits


2. $i is an auto-incrementing number, and the auto-incrementing number cannot exceed 8 digits


The result of the current simple analysis is the above. If you want global non-duplication, it is recommended to use uuid or the like.


Sample code:


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";
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template