- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
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!
Admin
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.
Admin
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?
Admin
"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.