0

I have a client which REQUIRES me to generate completely unique ids which NEVER, EVER, duplicate. He won't accept the fact that Guid.NewGuid() will statistically not repeat until the end of days https://stackoverflow.com/a/1705027/937131.

So I'm taking this as a challenge and tried to make a method which generates an ID which will never repeat a.k.a. a NONCE.

My general thought was that if I can incorporate a time portion at the end of every Guid, this should work.

Any ideas as to how this can be improved?

namespace JensB.Tools
{
    public class RandomGenerator
    {
        public string GetNOnce()
        {
            string unique = Guid.NewGuid().ToString();
            unique = unique.Replace("-", "");

            long unixTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            string timeString = ToBase62(unixTimestamp);

            unique = unique + timeString;

            return unique;
        }

        private string ToBase62(long input)
        {
            string baseChars = ALPHANUMERIC_ALT;
            string r = string.Empty;
            int targetBase = baseChars.Length;
            do
            {
                r = string.Format("{0}{1}",
                    baseChars[(int)(input % targetBase)],
                    r);
                input /= targetBase;
            } while (input > 0);

            return r;
        }

        private static string ALPHANUMERIC_ALT =
            "23456789" +
            "abcdefghjkmnpqrstuvwxyz";
    }
}
Community
  • 1
  • 1
JensB
  • 6,663
  • 2
  • 55
  • 94
  • 4
    Tell your client he's a paranoic and a complete layman. Create a wrapper that will literally return `Guid.NewGuid()` and bill the client for aditional work. – Tarec May 23 '14 at 09:33
  • 2
    @Tarec: While many of us would certainly like to do that when daydreaming, "business considerations prevent me from acting on that suggestion". – Jon May 23 '14 at 09:35
  • @Tarec He won't budge on that matter, so I'm just going to see if anyone has any ideas on a truly unique generator :) – JensB May 23 '14 at 09:35
  • @Tarec The client has access to the code and will see what I have done. – JensB May 23 '14 at 09:35
  • 2
    Presumably it needs to be a certain format/have certain randomness otherwise why not just have an incrementing value? –  May 23 '14 at 09:38
  • @DaveParsons That is correct, the id will be used in a URL that should be "impossible" to guess. – JensB May 23 '14 at 12:13
  • 1
    You could use an incrementing value and encrypt it. It will be both unique and unguessable. – Pierre-Luc Pineault May 25 '14 at 06:45

3 Answers3

1

You could maintain a list of all the previously generated GUIDs, say in a database, and then reject any that collide.

That way you can guarantee that all the GUIDs you generate are unique.

phuzi
  • 12,078
  • 3
  • 26
  • 50
1

Generate two GUIDS and add them together.... that will really really never ever duplicate!

One of the original ways of creating a guid involved the machines MAC address and the time. This was found to not be as reliable as people could possible guess a lot of it.

AnthonyLambert
  • 8,768
  • 4
  • 37
  • 72
1

I have a client which REQUIRES me to generate completely unique ids which NEVER, EVER, duplicate.

That's of course not possible. See the Pigeonhole principle.

A UUID is a 128-bit integer, so there can be 2^128 unique IDs. So if you generate 2^128 + 1 IDs, you have at least one duplicate.


For real world scenarios, Guid.NewGuid() is good enough.

sloth
  • 99,095
  • 21
  • 171
  • 219