- 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
75th
Admin
When i++ just won't do... When while loops are just too loopy... Maybe he should have used the structure suggested by the CAPTCHA: an enim (sort of like an enum, on a diet)
Admin
64th
Maybe somebody tried to explain the concept of UUIDs to him.
Admin
Admin
This one actually had me in hysterics! Generate three random numbers and hope one of them is unique - how genius is that? Of course, this code could easily be fixed by generating 48 random numbers, ensuring none of them clash and then performing the same logic. Bulletproof, right?!
Admin
TRWTF is.. Why non-sequential?
Admin
Third time isn't necessarily the charm... of course the developer wasn't that narrow-minded, that's why he included a catch-all:
So if the third time isn't the charm... the fourth time will be!
(Depending on how your sarcasm detector is configured, you may need to imagine a winking smiley here)
Admin
Probably some psuedo-requirement from someone who thought they were helping...
Admin
Admin
A little bit of calculation indicates that the tipping point when you'd start to expect a lot of trouble would be about 19. (You need to work out what the chance of finding a unique value is with four attempts, given how many unique values are already present.)
Of course, it could also just be ornery and give trouble on the fourth insert. Lovely.
Admin
Then, actually, TRWTF is that the code doesn't check the generated numbers to be compliant with this requirement =O
Admin
Maybe someone should explain the concept to you.
Admin
The real WTF is the requirement for a "unique non-sequential 2 digit identifier", Unless the Activity identifier only has to be unique within a specific period, and even then it's a little odd.
You would run out of unique identifiers after a few periods.
So many ways to do this. And all of them wrong.
It reminds me of the "randomly assign jobs" code we've been asked to implement. I'll do a full write up later.
Admin
What do you mean... It checks perfectly for all activities (for values of all less than 4 that is... ;-)
Yazeran
Plan: To go to Mars one day with a hammer
Admin
If not, just build a list of 48 numbers that don't repeat or form a sequence, and iterate through it - you can even pick a random start point if you want!
Admin
I've found that you can run out of unique identifiers after a number of missed periods.
Admin
I was really hoping to write the 3rd, 6th or 10th comment today, but those spots are all taken. Therefore I'll just be the 2nd.
Admin
No, you can't trust random number generators these days! What if it does provide 3 sequential numbers? What do you do then, huh?
Admin
Admin
It's a good thing they're checking activity.Cod. Can you imagine the smell if they checked activity.Haddock instead?
Admin
I couldn't help myself ...
With a new seed each hit/miss-iteration:
With a static seed:
With the method in question being used to generate these Cod's there still is some room for performance improvement! :D
Cheers!
Admin
Good thing here in Sweden the government provides an NSGUID for you if you wait for a couple of months after you've run out of periods ... :D
Admin
Nothing in the requirement stipulated random numbers. Random does not mean non-sequential. I agree with a previous poster implementing a lookup into an array of 48 suitably chosen two digit numerals. I would not infer anything that is not in the requirement.
Admin
i+=3 would work better - it would wrap around w/o repeating numbers until it used all 100 options (00-99)...
(captcha:sagaciter - someone you never want to get in a Star Wars argument with)
Admin
RTFA, it specifically says that the ids are unique for a given period. So no danger of running out of IDs.
Admin
I think those of you focusing on a list of 48 potential numbers are missing the fact that Periods may have up to 100 activities. The requirements are either inconsistent, or "non-sequential" was stated when it should have said "arbitrary".
Has anyone else noticed that the first three numbers generated need not even all be distinct? Further fail.
Admin
While this code is bad and stupid and ineffective but I can't see how it can generate many conflicts.
The risk of conflict is very small.
(Rounding a bit in my mat).
With 25 activities where are 3/4 change that a generated number is unique, and 1/4 that it have been used.
So the risk of generating 4 an ids already in use is 1/41/41/4*1/4=1/256 or something like 0.3%
Or did I miss something.
ps: The function does not generate 2 digit results for numbers 0 to 9.
Admin
haha
just hat a report about db4o uuid at school today ...
Admin
Indeed. Just ask George Foreman (and 5 of his 10 children who are also called George Foreman).
Admin
Actually, seeing how he doesn't check that the generated numbers aren't the same, you could get into trouble on the second insert.
Admin
[A Period can have anywhere from zero to forty eight Activities]
48 is adequate to meet the requirement.
Admin
Admin
TFA says:
You assumed that this meant unique within a period. It doesn't really say that though. If it said what you think it said it should say "each Activity should have a unique ... identifier within a Period". I agree that it's probably meant to say that, but TRWTF is, as always, the requirements.Admin
I would love to know what those requirements are for. Any solution that involves generating a random number has a chance of being wrong because true randomness can and will generate sequential numbers.
Admin
Here we go
[code]$id=sprintf('%02d',(1..48)[int(rand(47))+1]);[code]
Yes I know that Perl is the real WTF :)
Admin
Admin
Admin
However, by creating that array of 48 numbers, you've just defined a new sequence. From the first result off of googling "define:sequence":
serial arrangement in which things follow in logical order or a recurrent pattern
So if you set up a pattern like '02', '41', '99', ... for every period, then you're following a sequence. It may be a sequence of your own invention, but it still a recognizable sequence where '99' always follows '41' (except when the sequence is truncated to less than 3 elements), and so forth. It's a recurrent pattern that occurs over and over again for each period.
While I applaud your devotion to not inferring things that aren't in the spec, your solution fails the non-sequential rule. If the intent of the non-sequential rule was to prevent people from being able to easily enumerate the activities in each period by guessing sequential activity ids, then your solution has produced a less useful result.
Now one could argue that a liberal enough definition of sequence makes everything a sequence -- similar to the classic argument about how there are no uninteresting numbers. Still, I suspect that randomly chosen ids would satisfy the customer. However, the only truly proper solution would be to seek clarification on the requirement.
Admin
Can you have a non-random set of numbers that's not a sequence?
Admin
Admin
Take the worst case scenario: 47 out of 100 numbers have been taken. Then random guess #1 has a probability of failing of 47/100, #2 as well, #3 as well and #4 as well. The chance that they all fail is therefore (47/100)^4 = 5%. And that is assuming the random generator doesn't get reset...
Admin
[quote user="TGVish"][quote] The chance that they all fail is therefore (47/100)^4 = 5%.[/quote] And I forgot to add that it is cumulative. On the first round, failure probability is 0%. On the second, 1/100. On the third, approximately 2/100, etc. That means that by the time you reach a higher number, the probability that any one of the previous calls failed is pretty high. The most likely position to hit before it happens might be around 25...
Anyway, what idiotic system is this? Why is it needed? To avoid that people can guess the next number?
Admin
Nine. Nine. Nine.
Admin
Requirements are never complete and well-defined, never. That's why someone invented Scrum and iterative development and such nice things. However, if given requirements should have any sensible interpretation, I find it obvous that uniqueness was meant to be confined to a period.
There are ambiguities enough without beginning to make a lot more just to be smart.
Admin
If at first you don't succeed. try try and try again.
After that Fail!
Admin
TRWTF is using activity.Cod instead of activity.Flounder.
Admin
What TGVish is implying but not explicitly mentioning is how unlikely events become more and more likely to happen at least once over many tries. We're not worried about the probability of finding a unique ID for any given creation, we're worried about the overall probability.
Let's take something that has 1/100 probability of happening. That's pretty unlikely, if you take only one shot at it. But how likely is it if you try 20 times? To figure this out, you use negation. The probability of the event happening at least once is the complement of the probability of the event never happening.
Over 20 attempts, the probability of a 1/100 event happening at least once is (1 - (99/100)^20) = 18%. It kind of snowballs, by the way.
The probabilities in the WTF are a bit more complicated, but it's the same idea.
Admin
sequential:
Adapted From: WordNet 2.0 Copyright 2003 by Princeton University. All rights reserved.
sequential A adjective 1 consecutive, sequent, sequential, serial, successive
Please see the definition of "sequential", not "sequence".
Admin
Admin
static int lastGeneratedCode = 0; final int MAX_NUM_ACTIVITIES = 48;
public string GenerateNewActivityCode(Period period) { lastGeneratedCode+=2; return ... ; // add your favorite string conversion here }
Am I missing something? Non-sequential? Yes, the odd numbers are missing. Uniqueness? Of course.