Generating unique 6 digit code

前端 未结 6 2060
鱼传尺愫
鱼传尺愫 2020-12-28 10:06

I\'m generating a 6 digit code from the following characters. These will be used to stamp on stickers.
They will be generated in batches of 10k or less (before printing)

6条回答
  •  囚心锁ツ
    2020-12-28 10:57

    as Baba said generating a string on the fly will result in tons of collisions. the closer you will go to 80 millions already generated ones the harder it will became to get an available string

    another solution could be to generate all possible combinations once, and store each of them in the database already, with some boolean column field that marks if a row/token is already used or not

    then to get one of them

    SELECT * FROM tokens WHERE tokenIsUsed = 0 ORDER BY RAND() LIMIT 0,1
    

    and then mark it as already used

    UPDATE tokens SET tokenIsUsed = 1 WHERE token = ...
    

提交回复
热议问题