- 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
Admin
Admin
http://xkcd.com/221/
Admin
Admin
Any number in a sequence is ... sequential, so I'd say the solution method's body should be throw new StupidRequirementException();
Admin
Apparently people don't understand what a sequence is. http://en.wikipedia.org/wiki/Sequence
It is called the Fibonacci sequence, and that certainly isn't i++.
Also, did anyone note, 99 would never be valid (due to Random.Next being max exclusive).
While the 3 tries is a little strange, using a random number seems to be a valid way to go.
If you expect approximately half the space to be used in some cases, well I wouldn't use an approach that would take longer as more items are added.
Admin
Obviously the coder has never explored any card games. This cries out for a card deck shuffle algorithm - essentially each Period should be given a shuffled deck of 48 unique cards (picked from a deck of 99 cards); then assigning identifiers would merely be dealing off the top of the deck.
Doubling down would be optional, of course.
Admin
Yes, I assumed that meant unique within a period. I assumed that because that's what it actually says. I assume what the words said and what was actually wanted are in agreement.
Admin
Except that the random number thing doesn't work particularly well. Even with 4 random draws for each new ID in a period, there is a probability of about 22% that generating a new ID will fail on or before the 20th new ID in that period (with 1 random draw that would be 87%). That doesn't seem particularly valid.
Admin
Apparently people don't understand The Birthday Problem. http://en.wikipedia.org/wiki/Birthday_problem
You can't just use a random number blindly, or you will have collisions, which will cause interesting things to happen if the random number happens to be an identifying index. This is what makes hash tables interesting.
Random numbers are a good way to go, but you need some way to check for collision. For a small dataset, you can just use a lookup table. For a large dataset, where the memory cost of the lookup table is prohibitive, you can use a sorted list, or a hashtable, or etc... to reduce the complexity of lookup. As the article implies, there are plenty of valid solutions. This just isn't one of them.
Admin
Even before this method begins returning non-unique codes, it fails when returning codes < 10. ToString() doesn't automatically supply the leading 0.
Admin
The requirements don't say what many people seem to think they say. if you add 5 activities to an empty period, 1-5 is fine. Then you remove 3. On some definition, there's your non-sequential. With a lot of adding and deleting, you can even get non-monotonic sequences.
The job is not to produce 48 numbers that aren't in order. It is to deal with up to 48 that might not be.
Admin
Maybe someone should have explained the concept of IUDs to your mom.
Admin
Maybe his seed wasn't randomly generated?
Admin
I'm still trying to figure out what a non-sequential identifier is. Is "42" non-sequential ? Or "01" ? What about "34" ?
Admin
If you take the trouble to read it, you will find that what it actually says is "A Period can have anywhere from zero to forty eight Activities ...". Assuming you can read, that is, which may be the problem.
Admin
Admin
Cool. Is it old joke time? What do you call a person with no arms and no legs in the ocean?
Admin
You just need to apply a little data compression.
Admin
I bet there were a max of 3 activities in a period when this function was written. The requirement changed, but the function never changed.
Admin
Admin
Well, there are. But there are only 100 of them (or 90, if you don't count 05 as two-digit).
I use the number 11 by the way.
Admin
Everyone's arguing about the definition of a sequence but I think it is fair to infer that when the requirements said "non-sequential" they actually meant "non-linear". It can be argued that all numbers are sequential according to some sequence so clearly the intention of the requirements was to ensure that it was not a linear sequence, as in i = i + 1.
So TRWTF is the requirements spec, as always. But I think it is fair for us to get over the "non-sequential" bit and take it that they meant "non-linear".
Admin
Admin
[quote user="JR"]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.[/quote]
Yes, you missed "(e.g. 02, 41, 99)" - from the specs.
quote user="This would make a good Perl golf question"]Here we go
[code]$id=sprintf('%02d',(1..48)[int(rand(47))+1]);[code] [/quote]
[code]$id=sprintf('%02d',rand(98)+1);[code] Eagle!
Admin
Admin
[quote user="Anonymous"]Everyone's arguing about the definition of a sequence but I think it is fair to infer that when the requirements said "non-sequential" they actually meant "non-linear"...]
Nonlinear means an equation whose terms are higher than the first degree [ax^2 + bx + c]. Sequential means consecutive. Non-sequential=non-consecutive. Non-sequential<>nonlinear. The definition of 'sequence' has nothing to do with the requirement. The definition of 'sequential' does.
Admin
Admin
As long as there are no problems with maintaining state, here's how I'd do it. It won't win a code golf contest, but it's pretty easy to read and understand, even if you don't know Delphi:
Admin
Also, in various parts of South America, it's not unusual for a family to have more than one daughter whose first name is Maria. They'll often go by their middle names.
Admin
Clearly what is needed is a web service to generate the random numbers for you by passing in an XML document containing all the numbers previously used, then have the web service call itself recursively until the new, unique numbers are chosen.
I would have thought this was obvious.
Admin
TRWTF is the shockingly high percentage of programmers that can't read and understand these very simple requirements.
This is truly scary. The requirements were very simple, and pretty straightforward. Some people have been adding in their own requirements (must be random numbers), or flat out misinterpreting (100 events when the requirements say 48). And what is up with people questioning whether the identifier is supposed to be unique to a given period? Its pretty clear and obvious that was the intent of the requirement. Lets just slow the entire development process down with even more pedantic questions about things that are clear to anyone with half a brain. Thats the BSA and PMs jobs, not programmers!
Admin
But that's the same math I did, but I did not use the worst case senario because the bug repports said it would happend around 25 periods. Which give (25/100)^4=0.3%
Admin
That's the probability it fails on exactly the 25th run. The probability it fails then or before then is much higher.
Admin
Probably to reduce user-error (someone typing 04 instead of 05 and coding to the wrong activity).
Our old system ran 3-3-13 (1,4,7,20,23,26,39...), which in theory prevented one-off keying errors (never heard what the error rate actually was).
Admin
Still doesn't move the one key off keying error when using a numpad. Adding 3 will move the next one up a row more than half the time.
Admin
This is a trivial problem to meet the requirements, AS WRITTEN. I leave it up to the reader to expand it to the full 48 candidates.
Admin
Someone certainly missed probability 101. The chance of repeated birthdays if you pick 23 people at random is ~50%, but the chance of picking a repeated 24th birthday after you already have 23 people with guaranteed different birthdays is much lower (23/365). In fact, the calculation you did is identical to the GP's, he just did it for 25 instead of 47 since that's when they started seeing problems according to the OP.
Admin
I think the birthday problem is a result of the fact that people tend to get drunk and forget birth control the same times each year.
I have a lot of relatives whose birthdays are around the end of September. Happy New Year!
Admin
If my numbers are not wrong (that's very possible given that I haven't really paid attention).
The probability that it will makes it through a given number of runs (assuming all numbers are truly random) is in this google doc.
http://spreadsheets.google.com/ccc?key=0AmIbKrZ2YBMTdHV0aXpKTWJqTENvM2dKSF9DRDViSmc&hl=en
Admin
Admin
(Since we're ignoring the requirements anyway.)
Admin
I was thinking something along the same lines however if you wanted to make it seem more "random":
static List<string> sequence = new List<string> { "01", "03", "05", // ... etc "95", "97", "99"};
public static string GetNextSequence() { Random r = new Random(); int ix = r.Next(sequence.Count); string id = sequence[ix]; sequence.RemoveAt(ix); return id; }
It might have to be modified to meet specific needs but it would give id's in a seemingly random order that can't possibly be sequential and are guaranteed to be unique up to 50 items (in case the people writing the specification did want a sequence that could not be guessed at and didn't want sequential numbers such as "01" then "02", etc).
I still don't really understand the requirements though in the sense that I can't see how something like that could possibly be useful.
I'm thinking of a context like Period A has Activiy A and Activity B. Period B has Activity A. With the specification as it is Activity A could have an ID of "02" in Period A and an ID of "97" in Period B. Is that useful? I don't know, maybe? I would think that if each Activity had it's own unique ID relative to other Activities you wouldn't need all this other mess. Maybe I am just over-thinking it though @_@
Admin
Admin
Maybe whoever wrote the requirement was a ransom negotiator.
Give me 48 activities in non-sequential numbers or your code gets it.
Admin
This could potentially be from management being concerned about security and wanting it randomized. Still doesn't justify the poor implementation.
Admin
Oh...so that's how you solve the problem of like Marias.
Admin
However, in programming "a sequence" and "a sequential run of numbers" are not necessarily same thing, so it's perfectly normal to have a "non-sequential sequence". The meaning of "non-sequential sequence" I leave as an exercise to the reader (if you can't answer, I suggest avoiding professional software development, mostly for the sake of your own sanity).
Admin
If I asked a client whether random numbers would be an acceptable "non-sequential" way of getting two-digit IDs or if he should clean up his specification, I would be fired on the spot for wasting someone's time over trivial nonsense. Obviously random is a perfectly acceptable implementation.
Admin
I read through this quickly, so perhaps I'm missing something, but it seems to me that you could just generate an array of 48 unique numbers and shuffle them. Once a number has been used, remove it from the array. Once all the numbers are used, start over.
I've written a chunk of PHP that grabs the filenames of images from a specific folder, dumps the filenames into an array, shuffles the array, and then prints out the value of the first index when a webpage loads. Then it just removes that index from the array. The array is generated with a new browser session or once the array is empty.