- 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
Why is this a very bad way? It looks a lot like the Python and Java examples given on that page, except it starts at the beginning of the array and moves forwards instead of starting at the end and counting backwards.
Admin
groan OK, you win today's thread.
Admin
Not quite. Your algorithm can swap given numbers multiple times. The proper way 'picks' a number and then puts it at the end of the list, then repeats for the list of 1 to n-1. Once an element is placed at a position it cannot be moved again.
Here's why yours isn't as valid.
Admin
// my wife doesn't do any activities while period is present exit;
Admin
Oh! I didn't notice that part. Thanks for pointing that out. I'll have to remember that.
(Wow! Who'da thunk I'd actually learn a way to improve the quality of my code on The Daily WTF?)
Admin
Admin
first decent suggestion on here, congrats. The solution I had in mind is to have a list of all numbers and just pop a random one from it when needed.
Admin
Admin
Here's a simple non-sequential sequence in three lines:
int result=50-previousEntry(); if(result<=0) --result; return 50+result;
And a small expansion in case previousEntry() returns numbers from the previous period gives:
int result=50-previousEntry(); if(result<=0) --result; if(result==50) return 50; else return 50+result;
Produces: 50,49,51,48,52,...
Admin
Why not? Just stop the loop at a random value
for{i = 0; i < Math.Rand(99); i++}
or better
for(i = 0; i < 65535; i++) // Prepare for higher values { ... if(i = Math.Rand(99)) break; ... }
Should be ok, no?
CAPTCHA: decet, makes me think of fecet
Admin
Well apart from the fact that
for{i = 0; i < Math.Rand(99); i++}
should be
for(i = 0; i < Math.Rand(99); i++) // I'm sorry it's a bit late
Maybe we could, at that occasion, set up a dedicated random number server, returning lots of XML?
Admin
Nope. All four random number could be the same. This could have a dupe ID on the second activity, though it's very unlikely. The spec just says non-sequential... you could just increment it by some arbitrary prime number each time and then mod 100 it.
Admin
Will this have to run on an embedded system?
Admin
I'm glad I don't have to write requirements documents. The sheer amount of pedantry and inability to comprehend written statements would cause my head to explode.
Admin
Or even more ornery and give trouble on the second insert. the same value 4 times in a row is still random.
Admin
Nonsequental numbers are often used if manual typing is involved.
It is done to ensure that the typist actually has to look at the screen, reading the info instead of just repeating the procedure from the previous record.
Admin
Given that he makes a new pseudo-RNG every time it's more likely than you think. The WTF is that it doesn't fail on the second call.
Admin
Doesn't SQL have a function for this?
He should have used SQL...problem solved!
Admin
Moreover, there are many sets of numbers that are uncountable, e.g. the set of real numbers, and thus cannot be expressed by even an infinite sequence. IOW epic fail!
Admin
Hey, it meets all the must requirements.
Admin
Um, you DO know that indexing linked lists is horribly inefficient? You just turned an O(n) algorithm into O(n^2).
(assuming that TList is a linked list, apologies if I'm mistaken)
Admin
Asserts can be compiled out. Don't rely on them in your program logic.
Admin
Admin
Delphi implements TList as an array of pointers, so indexing is the right way.
Admin
In almost any language you can meet the spirit requirements in one line:
return lastCode + Math.Rand(2);
That's as good as any other solution that has been posted.
However, it makes more obvious the point that making a truly non-sequential number generator is mathemetically impossible, as you can come up with a sequence definition for any amount of numbers. As a simplified example, a sequence of {5,99} is the sequence defined as "start with 5, then each iteration add 94 to the previous number".
Admin
Just generate a random amount of numbers and cross-reference with The On-Line Encyclopedia of Integer Sequences:
http://www.research.att.com/~njas/sequences/
Yes it actually exists. No idea why though.
Admin
How does he know that his random numbers are not themselves sequential?
So many things wrong with this.
Admin
Admin
Admin
You always can, you just have to not define an order. That is also a sure way of creating a non-sequence with a set of random numbers.
Admin
+1 for a simple, elegant solution. Most of the suggestions and code sample on this thread look like jokes to me.
Admin
non·lin·e·ar (nŏn-lĭn'ē-ər)
adj.
Not in a straight line.
Containing a variable with an exponent other than one. Used of an equation.
The American Heritage® Dictionary of the English Language, Fourth Edition Copyright © 2009 by Houghton Mifflin Company. Published by Houghton Mifflin Company. All rights reserved.
sequential:
Adapted From: WordNet 2.0 Copyright 2003 by Princeton University. All rights reserved.
sequential A adjective 1 consecutive, sequent, sequential, serial, successive
I did look them up in a dictionary, not a math book, did you?
Last time I checked, computer programs are based on math, not the english language. I found no reference defining "natural definition".
Admin
The implementation of "non-sequential" depends on why non-sequential numbers are required. This could be a case of "suggested solution masquerading as a requirement" - in my experience that's a good way of producing needlessly complicated code, or at least code that you have to go back and redo because you weren't given the real requirement.
If the real requirement is "so you can't tell how many activities there are from the activity numbers", then random + collision detection is probably the best choice. You might get two or three consecutive numbers from a PRNG, but it doesn't matter because "non-sequential" isn't the real requirement.
If the real requirement is "to reduce keying errors" then something like 17n mod 100 would do.
If the real requirement is "to fill up the space of IDs evenly" then how about int(61.80334n) mod 100, based on the golden section - pretty good for reducing keying errors too.
The last two aren't "non-sequential", in that you can work out the next number, or see how many activities there are from which numbers are used - but again that wasn't the real requirement.
Admin
++activity_number ^ 0b10101
Admin
I asked a friend how he would code it to meet the requirements, and he said take the activity index (1st call =1, 2nd call=2...) format it to to numerals and reverse the numerals. Elegant and meets the requirement. And nothing more.
Admin
I'm not sure if the original code is Java or C# (they both look pretty similar, and I've never had need to use a random function in .NET), but it seems like a collection could be created with a random number for each activity.
Once the collection is filled, it could be iterated through to see if the current number in the .Index == the previous number in the .Index +1 or any other number in the collection. Then just keep repeating until the random number satisfies the condition.
It's a lot of work for a simple array or collection, but with the upper bounds of 48 activities, I doubt it would actually take much compute time.
Admin
That code looks like offal. It doesn't guarantee uniqueness and it doesn't guarantee non-sequential. It'll work for the average case of three or few activities, but it's not guaranteed to work beyond that.
If I had to try to fill the requirement, I'd generate a random number then check it against all the existing activity numbers with a special check against the previous activity number that the new number not equal it but also not equal it+1.
I'm not saying that's the best approach, but that's all I've ever been able to think up for that crazy non-sequential requirement. I'd like to believe there's a more efficient way.
But TRWTF is the ^requirement^! What the heck--most have three Activities, but there could be forty-eight!? That's gotta be a government project.
Furry cows moo and decompress.
Admin
The 1/256 reminded me of hacking around on Doom. You could alter a monster's chance for flinching in pain when hit with an attack. The probability was x/256 where x was the number you could change in the editor.
Yeah, 1/256 is pretty darn rare, but it depends on how long you're going to shoot the monster or how many total period/activity sets you're generating.
Remember, 0.3% of the population of Earth would still represent 18,000,000 people.
Furry cows moo and decompress.
Admin
Admin
What requirements? I didn't see a [b]shall[\b]
Captcha: decet - That was a pretty decet attempt at a requirement.
Admin
Your argument is that you wish to use the definition "Not in a straight line" as a replacement for "non-sequential" in the requirement?
The numbers..must not be in a straight line...
What does that mean?
Admin
Wow, judging from the pedantic comments on the definition of "sequential", it sounds to me like you view requreiments definition as a kind of contest, where the user tries to explain what he wants as clearly as possible, and you try as hard as you can to come up with some way to interpret his words that can be defended on literal linguistic grounds, but is as far from the user's obvious intended meaning as possible. Like, insist on using technical mathematical definitions of words instead of common meanings, even though the user is not a mathematician. Like, the user says he wants "a set of fields on the screen showing the customer name, address, city, state, and zip". Well, a "set" is unordered, so this must mean that the fields should appear in a different order every time they're displayed!
Admin
At the risk of being serious, and the further risk of being obvious, it seems to me that the right way to implement what the programmer apparently intended would be something like:
Oh, and assuming that "01", "02" etc count as two-digit numbers ...
Admin
Or just use something like: unique = index * 2
Admin
Answering a wtf with a wtf.
To use: When you create a new period:
To create an activity:
WORN (write once read never) code is always best for wtf
Admin
Find the one missing char :-)
Admin
Really enjoyed this post.Really thank you! Keep writing. makaberzux