• (nodebb)

    Whilst still not guaranteed to be unique (so still satisfying the requirement of being "Good Enough, so will still mysteriously fail sometimes), what's wrong with this

            private static RNGCryptoServiceProvider m_random = new RNGCryptoServiceProvider();
            //private static Random m_random = new Random(); //Don't do this, Random is seeded with the current value of the clock, so
            //if two concurrent processes or machines create this object within the same 15ms, it will yield the same predictable sequence
            //guaranteeing clashes
    
            public static string Get5RandomNumbers()
            {
                byte[] buf = new byte[sizeof(uint)];
                m_random.GetBytes(buf);
                var number = BitConverter.ToUInt32(buf, 0) % 99999;
                return number.ToString("D5");
            }
    

    Addendum 2019-10-31 03:20: Yes, I'm also aware that this method is slightly biased towards lower numbers as there uint.MaxValue is not exactly divisible by 99999, but we're talking "Good Enough" here

    Addendum 2019-10-31 03:28: Oops, I also realised it should have been 6 random numbers. Bad Code Monkey!

  • Jaime (unregistered) in reply to Rick Poleshuck

    Rick, I know that the Wikipedia article says VINs have a standard, but that's a standard that manufacturers follow. In the case of a home-built vehicle, I'm the manufacturer and I'm the one creating the VIN. The DMV doesn't tell me to follow the standard and doesn't care if I do.

  • (nodebb) in reply to Barf4Eva

    You said "proper mix of natural keys and surrogate keys" so in some cases a natural key is better than a surrogate key. Can you name one example where that holds true?

  • nasch (unregistered)

    "Many years ago, a researcher created a database using patient's medical record numbers as primary keys, which is a good natural key. "

    That's not a natural key, it's a surrogate key. An example of a natural key would be the patient's name.

Leave a comment on “To Be Random Enough”

Log In or post as a guest

Replying to comment #:

« Return to Article