- 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
There's almost certainly a "Things programmers don't understand about random numbers" compilation out there somewhere. Not gonna bother looking. But Rule 1 ought to be " 'Random' and 'unique' are two unrelated ideas. NEVER mix them up."
Admin
What's interesting is the answer to "if I used a sampler with replacement, how many for that 50% chance of duplicates?"...
And that answer is "379, duh." Conclusion: it's still probably not enough to be "safe", unless it's for just a small handful of VMs.
Admin
Remember, one of the weaknesses of the Enigma Machine was that it never mapped a input character to itself. That hard-coded rule was the chink that let in the codebreakers.
Admin
what surprises me is that to get to this relatively esoteric function, they must have read past the documentation about plain old random value generation and thought to themselves 'no that's not what I want' !?!?
Admin
Does this application really need the unique IDs to be unpredictable? If not, there's no need for random generation in the first place, just assign sequential numbers.
Admin
And to add to what you said, it should probably be handled on the database end (assuming they have one.)
Admin
@Tim R ref
My take is this is what happens when everything in your mental world is a string, not a strongly-typed object. They thought of the ID as a string and only as a string. So they needed a stringly generation algorithm. So they cobbled one together from concatenation.
HTML, JSON, Javascript, etc. Heck, just client/server over http. Everything goes to/from stringly typed data at least once, if not 6 times on the way from one end of the entire app stack to the other. It's the poisoned gift that keeps on destroying what we've known for decades about how to create robust maintainable code that's got heavy safeguards against type misuse.
Admin
I second this. I know plenty of people that have decided that since JSON doesn't have a native datetime data type, that it's obviously unnecessary. They argue that date/time data can be represented as strings using ISO 8601 rules. They forget that this addresses part of the problem, but 8601-in-JSON does not provide any mechanism to ensure that the string in the value is valid.
Admin
You just have to wonder, if at some point some manager or user saw the numbers and complained that they didn't "look random enough", and so the developer had to change it to not be able to repeat digits.
Admin
They could have gone with
return f"{random.randint(0,10**digits):0{digits}}"(and passed indigitsas a defaulted parameter).I think CS classes try to hammer home the birthday problem, but when you are sitting at your desk scrambling for a unique identifier without setting up some central database service to do so, random often rears its head... and the concept gets reinforced by UUID-v4, but it has 122 random bits and not just 16.6...
It remains an annoying problem due to having to set up a database, be resilient to restarts and corrupted data, and requiring coordination...
Admin
There's no mechanism in JSON to ensure that any value is "valid" beyond being a decimal number, string, boolean, untyped array or object.
Admin
Ah yes, I once worked on a web based application where they generated random session ids and the range was effectively 2^64. It took only a few weeks before the first defect reports of people seeing data belonging to other unrelated people started to come in.
Admin
Ummm.. that's the point. If a serialization format supports a date datatype, then any attempt to de-serialize a date that doesn't contain date data will error. JSON was originally (and is often still) used schema-less where the de-serializer just gives you whatever is there. Good serialization formats do validation at deserialization time.