Creating your own TinyURL

前端 未结 13 2127

I have just found this great tutorial as it is something that I need.

However, after having a look, it seems that this might be inefficient. The way it works is, fir

13条回答
  •  执念已碎
    2020-12-13 12:11

    I don't know why you'd bother. The premise of the tutorial is to create a "random" URL. If the random space is large enough, then you can simply rely on pure, dumb luck. If you random character space is 62 characters (A-Za-z0-9), the the 4 characters they use, given a reasonable random number generator, is 1 in 62^4, which is 1 in 14,776,336. Five characters is 1 in 916,132,832. So, a conflict is, literally, "1 in a billion".

    Obviously, as the documents fill, your odds increase for the chance of a collision.

    With 10,000 documents, it's 1 in 91,613, almost 1 in 100,000 (for round numbers).

    That means, for every new document, you have a 1 in 91,613 chance of hitting the DB again for another pull on the slot machine.

    It is not deterministic. It's random. It's luck. In theory, you can hit a string of really, really, bad luck and just get collision after collision after collision. Also, it WILL, eventually, fill up. How many URLs do you plan on hashing?

    But if 1 in 91,613 odds isn't good enough, boosting it to 6 chars makes it more than 1 in 5M for 10,000 documents. We're talking almost LOTTO odds here.

    Simply put, make the key big enough (7 characters? 8?) and the problem pretty much "wishes" itself out of existence.

提交回复
热议问题