首页 > 京东上生成100万张优惠券算法问题

京东上生成100万张优惠券算法问题

京东上生成100万张优惠券,优惠券券号前缀是QA(比如QAK001,QA0U767,QAJUT55,随机的券号),用什么算法能生成100万张不重复的优惠券?这100万张优惠券券号必须存入数据库或缓存中吗?


可以参考算法Bloom Filter


额,并不是回到问题,继续追问楼主的第二个问题呢:“这100万张优惠券券号必须存入数据库或缓存中吗?” ,望这方面有经验的大神解惑


推荐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;
    }
}
【热门文章】
【热门文章】