0

I know I can use GUID to generate a unique string, but it's too long. Now I only need generate un-duplicate string within an website, how can I do? Thanks!

For example: In the website http://mathurl.com/, you can generate a permanent URL, such as http://mathurl.com/75ujy7b and 75ujy7b is very short and un-duplicate.

  • Numbers can be "un-duplicate". So can numbers in base 36. They are also incremental, and thus easy to generate ;) – K Mehta Dec 16 '11 at 01:15
  • @Paul You might also want tell us what language you are doing this in, c#, vb.net, etc. – Stephen Dec 16 '11 at 04:01

2 Answers2

2

First of all you can start with a counter that you make sure using Mutex/lock that return unique incremental numbers, and you save the last number somewhere on your site, maybe in web.config, maybe in database, or in a file.

Then you convert this unique number to a different base number, eg to a base-64. Here is some code, and mode details on how you can do that

https://stackoverflow.com/a/5901201/159270

And you can get results like

value: 0 encoded: A
value: 1 encoded: B
value: 9999999999 encoded: SrYsNt
value: 4294965286 encoded: ZNGEvT
value: 2292964213 encoded: rHd24J
value: 1000000000 encoded: TrNVzD

Now, if you scramble the map on the characters you can also make a not so easy to find number.

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
0

you can use a random number of sufficient length.. between lets say 10,000 and 99,999..

if that aint good enough for you, you could look into some hashing algorithms.. i think..

AweSIM
  • 1,651
  • 4
  • 18
  • 37
  • 1
    Just because your using a random number doesn't stop the number being duplicated. (You can and will end up with the random number generating a number that has already been used) – Stephen Dec 16 '11 at 03:59