<?php
/**
* 取余
* @param string $userId
* @return int
*/
function getRemainder(string $userId)
{
// return $remainder = $userId % 100; #使用userId取模获取,比如放量x%:$remainder < x
$key = "Z1SbMB";
// 对userId进行加盐(key),然后md5加密
$md5Str = md5($key . $userId);
// 这个地方取前6位进行转换,因为取全部,算出来太长
$subStr = substr($md5Str, 0, 6);
// 将16进制转换为10进制,再对100取余
return hexdec($subStr) % 100;
}
$hit = 2; // 命中率
$count = 0;
$hit_count = 0;
$file = fopen("users.txt", "r");
while (!feof($file)) {
$userId = fgets($file);
$userId = str_replace("\r\n", "", $userId); // 去掉换行符
$userId = str_replace("\"", "", $userId); // 去掉"
$remainder = getRemainder($userId);
if ($remainder < $hit) {
echo $userId . ':' . $remainder . ",";
echo "命中" . "\n";
$hit_count++;
} else {
echo $userId . ':' . $remainder . ",";
echo "丢失" . "\n";
}
$count++;
// if ($count >= 100000) break;
}
fclose($file);
echo "hit_count" . ":" . $hit_count . "\n";
echo "count" . ":" . $count . "\n";
exit;
$hit = 1; // 命中率
//$userId = strval(2);
//$userId = strval(rand(2086846789, 2086946789));
$hit_count = 0;
for ($i = 0; $i < 100000; $i++) {
$userId = strval(2086846789 + $i);
$remainder = getRemainder($userId);
if ($remainder < $hit) {
echo $userId . ':' . $remainder . ",";
echo "命中" . "\n";
$hit_count++;
} else {
// echo "丢失" . "\n";
}
}
echo "hit_count" . ":" . $hit_count . "\n";
// mysql
// conv(substr(md5(concat('Z1SbMB',0)),1,6),16,10)%100
// select id,conv(substr(md5(concat('Z1SbMB',id)),1,6),16,10)%100 as number from user where id = 2;
逐行读取txt文件-PHP
最新推荐文章于 2024-04-14 05:15:00 发布