Is there a simple way to generate a un-duplicate string?

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.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Solution 2

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..

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply