时间:2021-07-01 10:21:17 帮助过:2人阅读
京东上生成100万张优惠券,优惠券券号前缀是QA(比如QAK001,QA0U767,QAJUT55,随机的券号),用什么算法能生成100万张不重复的优惠券?这100万张优惠券券号必须存入数据库或缓存中吗?
可以参考算法Bloom Filter
推荐UUID。
function guid(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = chr(123)// "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12)
.chr(125);// "}"
return $uuid;
}
}