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

前端 未结 8 1378
时光说笑
时光说笑 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:39

    I asked basically the same question recently, and the solution was to increment by a prime number (modulo max) to get a nice seemingly random sequencing w/o repeating any numbers: Tinyurl-style unique code: potential algorithm to prevent collisions

    0 讨论(0)
  • 2020-12-04 23:40

    It depends what you mean by unpredictable. If you want cryptographically secure, you might be interested in the Blum Blum Shub algorithm, but you probably don't.

    I have implemented a linear feedback shift register for the purpose of giving random looking unique identifiers. LFSRs are simple to implement and they cycle through all possible combinations, although it is possible to calculate the next number given the previous number (it isn't straight forward, but it can be done).

    I'm not sure how to use the entire 26^6 space if you use a LFSR. A LFRS is of a certain bit length and cycles through every possible combination of those bits (except 00...0 I think). You could use a 28 bit LFSR, but you would lose the top 40 million combinations (which is about 13% of them).

    It appears that it is possible to map the states of the LFSR with ordinals (i.e., the nth state of the LFSR is x), but there's a patent on it... But you want to go in reverse anyway.

    0 讨论(0)
提交回复
热议问题