求 16 位纯数字订单号产生算法
现在用的 18 位:
<?php declare(strict_types=1);
date_default_timezone_set(‘Etc/GMT-8’);
$time = microtime(true);
function order()
{
$ids = 0;
$aes = substr(microtime(), 2, 6);
$pid = $aes . getmypid() . mt_rand(0, 999999);
for($in = 0; $in < 18; $in++) {
$ids += (int) (substr($pid, $in, 1));
}
return time() . $aes . str_pad(strval($ids), 2, ‘0’, STR_PAD_LEFT);
}
$arr = array();
for($i = 0; $i < 1000000; $i++) {
$arr[] = order();
}
$arrs = array_count_values($arr);
echo ‘COUNT ‘ . count($arrs);
echo ‘<br>’;
echo ‘RAND ‘ . array_rand($arrs);
echo ‘<br>’;
arsort($arrs);
$as = [];
foreach($arrs as $ids => $aos) {
if ($aos > 1) {
$as[] = $ids;
echo $ids . ‘ – ‘ . $aos . ‘<br>’;
}
}
echo ‘COUNT ‘ . count($as);
echo ‘<br>’;
echo ‘Time ‘ . number_format((microtime(true) – $time), 3);