Map incrementing integer range to six-digit base 26 max, but unpredictably

前端 未结 8 1382
时光说笑
时光说笑 2020-12-04 22:39

I want to design a URL shortener for a particular use case and type of end-user that I have targetted. I have decided that I want the URLs to be stored internally according

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 23:19

    26^6 is around 300 million.

    Easiest just to use a random number generator, and if you have a collision (i.e. in case your randomly generated 6-letter identifier is already taken), increment until you have a free identifier.

    I mean, sure, you'll get collisions fairly early (at around 17 thousand entries), but incrementing until you have a free identifier will be plenty fast, at least until your keyspace starts to be saturated (around 12 million entries), and by then, you should be switching to 7-letter identifiers anyway.

提交回复
热议问题