• (cs) in reply to fjf
    fjf:
    Mason Wheeler:
      for i := 0 to max do
        FList.Exchange(i, Random(max + 1));
    
    For an application liks this that doesn't actually require randomness, this might be alright, but in general that's a very bad way, see here.

    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.

  • (cs) in reply to Bumble Bee Tuna
    Bumble Bee Tuna:
    Mason Wheeler:
    Anon:
    the beholder:
    Jellineck:
    Medezark:
    You would run out of unique identifiers after a few periods.
    I've found that you can run out of unique identifiers after a number of missed periods.
    When my wife missed her periods we had to start thinking on an identifier. It didn't have to be unique, though.

    Indeed. Just ask George Foreman (and 5 of his 10 children who are also called George Foreman).

    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.

    Oh...so that's how you solve the problem of like Marias.

    groan OK, you win today's thread.

  • Hatterson (unregistered) in reply to Mason Wheeler
    Mason Wheeler:
    fjf:
    Mason Wheeler:
      for i := 0 to max do
        FList.Exchange(i, Random(max + 1));
    
    For an application liks this that doesn't actually require randomness, this might be alright, but in general that's a very bad way, see here.

    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.

    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.

    Similarly, always selecting k from the entire range of valid array indexes on every iteration (i.e. using k = rng.nextInt(array.length) in the Java example above) also produces a result which is biased, albeit less obviously so. This can be seen from the fact that doing so yields NN distinct possible sequences of swaps, whereas there are only N! possible permutations of an N-element array. Since NN can never be evenly divisible by N! (as the latter is divisible by N−1, which shares no prime factors with N when N > 2), some permutations must be produced by more of the NN sequences of swaps than others
  • anon (unregistered)

    // my wife doesn't do any activities while period is present exit;

  • (cs) in reply to Hatterson
    Hatterson:
    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.

    Similarly, always selecting k from the entire range of valid array indexes on every iteration (i.e. using k = rng.nextInt(array.length) in the Java example above) also produces a result which is biased, albeit less obviously so. This can be seen from the fact that doing so yields NN distinct possible sequences of swaps, whereas there are only N! possible permutations of an N-element array. Since NN can never be evenly divisible by N! (as the latter is divisible by N−1, which shares no prime factors with N when N > 2), some permutations must be produced by more of the NN sequences of swaps than others

    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?)

  • Bear (unregistered)
    // no period may have more than 48 activities. 
    const int MAXACTIVITIES = 48;
    
    // all activity codes must be between 0 
    // and 99 inclusive.  
    const int MAXCODE = 99;
    
    public string GenerateNewActivityCode(Period period)
    {
       // if this assert fails it means that the 
       // number of allowed activities per 
       // period is now so great that it is 
       // not possible to generate legal 
       // activity codes for them all.
       assert(MAXACTIVITIES <= MAXCODE);
    
       Random rand = new Random();
       int codenum = rand.Next(MAXCODE);
       int stepper; 
       string code;
       bool inuse;
       do {
         inuse = FALSE
         code = codenum.ToString();
         foreach 
           (Activity activity in period.Activities)
            if (activity.Cod.Equals(code))
                inuse = true;
          else {
             // 53 or 59 because they are 
             // prime -- all possibilities will 
             // be tested, no matter the value of 
             // MAXCODE.
             codenum += (53 + 
                 (6 * (MAXCODE % 53 == 0)));
             codenum %= MAXCODE;
             stepper += 1;
             }
        } while (inuse && 
                 (stepper < MAXACTIVITIES));
        if (!inuse) return(code);
        else 
    
        // If this assert is ever executed, it 
        // means that a period has more than 
        // MAXACTIVITIES activities (we exited the 
        // loop without finding an activity code, 
        // after MAXACTIVITIES attempts).
    
        assert( 1 == 0 );
    
        // this'll never happen due to the 
        // above assert, but it shuts up the 
        // compiler about not returning a value.
    
        return(0);
    }
    
  • Boss (unregistered) in reply to Mean Mr. Mustard
    Mean Mr. Mustard:
    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.

    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.

  • (cs) in reply to Anonymous
    Anonymous:
    wtf:
    When i++ just won't do...
    Well, the specs *said* the number should be non-sequential. Hence, i++ just won't do, indeed.
    Should != must.
  • Tyler (unregistered)

    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,...

  • ClaudeSuck.de (unregistered) in reply to Anonymous
    Anonymous:
    wtf:
    When i++ just won't do...
    Well, the specs *said* the number should be non-sequential. Hence, i++ just won't do, indeed.

    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

  • ClaudeSuck.de (unregistered)

    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?

  • RyanC (unregistered) in reply to Yazeran

    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.

  • (cs)

    Will this have to run on an embedded system?

  • scjohnno (unregistered) in reply to somedude
    somedude:
    TRWTF is the shockingly high percentage of programmers that can't read and understand these very simple requirements.
    Totally unsurprising, given: http://thedailywtf.com/Comments/Riddle-Me-An-Interview.aspx

    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.

  • Dysan (unregistered) in reply to dkf
    dkf:
    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.

    Or even more ornery and give trouble on the second insert. the same value 4 times in a row is still random.

  • Deepcore (unregistered) in reply to DocBrown

    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.

  • Jimmy Jones (unregistered) in reply to dkf
    dkf:
    A little bit of calculation indicates that the tipping point when you'd start to expect a lot of trouble would be about 19.

    Of course, it could also just be ornery and give trouble on the fourth insert. Lovely.

    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.

  • Jimmy Jones (unregistered)

    Doesn't SQL have a function for this?

    He should have used SQL...problem solved!

  • AdT (unregistered) in reply to Paster
    Paster:
    Charleh:
    Can you have a non-random set of numbers that's not a sequence?
    If you want to be pedantic, then if it's a set, it by definition isn't a sequence, because a set doesn't have any specific order.

    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!

  • AdT (unregistered) in reply to wjr
    wjr:
    Should != must.
    public string GenerateNewActivityCode(Period period)
    {
      return "mu";
    }

    Hey, it meets all the must requirements.

  • dezgeg (unregistered) in reply to Mason Wheeler
    Mason Wheeler:
    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:
    unit RandomSequence;
    
    ...snip
    
        FList: TList<integer>;
    ...snip
      for i := 0 to max do
        FList.Exchange(i, Random(max + 1));
    ...
      result := format('%.2d', [FList[FCounter]]);
    

    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)

  • Amtep (unregistered) in reply to Bear
    Bear:
        [... code snipped ...]
    
    assert( 1 == 0 );
    
    // this'll never happen due to the 
    // above assert, but it shuts up the 
    // compiler about not returning a value.
    
    return(0);
    

    }

    WTF

    Asserts can be compiled out. Don't rely on them in your program logic.

  • Anonymous (unregistered) in reply to asdf
    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"...
    asdf:
    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.
    That's the mathematical definition. I meant the natural definition, try looking it up in a dictionary instead of your maths textbook.
  • AndersI (unregistered) in reply to dezgeg
    dezgeg:
    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)

    You are mistaken. Apology accepted.

    Delphi implements TList as an array of pointers, so indexing is the right way.

  • Anonymous (unregistered)

    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".

  • Mr Gangreen (unregistered)

    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.

  • Anon Too (unregistered)

    How does he know that his random numbers are not themselves sequential?

    So many things wrong with this.

  • (cs) in reply to wtf
    wtf:
    Maybe he should have used the structure suggested by the CAPTCHA: an enim (sort of like an enum, on a diet)
    The first in a series of the enim: ENIMA
  • (cs) in reply to Anonymous
    Anonymous:
    Steve Johnstone:
    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.
    Bear in mind that the requirements said nothing about the IDs being random. It was the idiot coder who decided that was a good idea, it certainly never came from the requirements.
    As an idiot coder I must say, requirements come from idiot users.
  • (cs) in reply to Charleh
    Charleh:
    Can you have a non-random set of numbers that's not a sequence?

    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.

  • (cs) in reply to Boss
    Boss:
    Mean Mr. Mustard:
    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.

    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.

    +1 for a simple, elegant solution. Most of the suggestions and code sample on this thread look like jokes to me.

  • asdf (unregistered) in reply to Anonymous
    Anonymous:
    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"...
    asdf:
    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.
    That's the mathematical definition. I meant the natural definition, try looking it up in a dictionary instead of your maths textbook.

    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".

  • (cs)

    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.

  • Faxmachinen (unregistered)

    ++activity_number ^ 0b10101

  • asdf (unregistered)

    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.

  • (cs)

    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.

  • Wyrd (unregistered)

    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.

  • Wyrd (unregistered) in reply to tiller

    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.

  • Anonymous (unregistered) in reply to asdf
    asdf:
    Anonymous:
    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"...
    asdf:
    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.
    That's the mathematical definition. I meant the natural definition, try looking it up in a dictionary instead of your maths textbook.

    non·lin·e·ar (nŏn-lĭn'ē-ər)
    adj.
    Not in a straight line.

    I rest my case. Thank you for so eloquently proving it for me.

  • Unrolled Loop (unregistered)

    What requirements? I didn't see a [b]shall[\b]

    Captcha: decet - That was a pretty decet attempt at a requirement.

  • asdf (unregistered) in reply to Anonymous

    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?

  • Jay (unregistered)

    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!

  • Jay (unregistered)

    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 ...

    public Integer getNextActivityNumber(List activityNumbers)
    {
      // Keep trying random numbes until you find one
      // that isn't used.
      do
      {
        Integer n=Integer.valueOf(Random.nextInt(100));
      } while (activityNumbers.contains(n));
      return n;
    }
    
  • (cs) in reply to Anonymous

    Or just use something like: unique = index * 2

  • Tim (unregistered)

    Answering a wtf with a wtf.

    function generateActivityNumber(short count, short* list) {
        short x = count*rand();
        if(list[x]) list[x]=x+1;
        if(x) {
            list[x]^=*list;
            *list^=list[x];
            list[x]^=*list;
        }
    }    
    

    To use: When you create a new period:

    short activityList[100];  /* If you dynamically allocate
                                 you'll have to zero */
    short numActivities = 0;
    activityList[0] = 1;
    

    To create an activity:

    generateActivityNumber(numActivities, &activityList[numActivities]);
    printf("%02d", activityList[numActivities]-1);
                                          /* or sprintf to                                            
                                             some string */
    numActivities++;
    

    WORN (write once read never) code is always best for wtf

  • Tim (unregistered) in reply to Tim

    Find the one missing char :-)

  • big tits incest sex (unregistered)

    Really enjoyed this post.Really thank you! Keep writing. makaberzux

  • mom son incest (unregistered)
    Comment held for moderation.
  • young incest porn (unregistered)
    Comment held for moderation.
  • incestsex.vip (unregistered)
    Comment held for moderation.

Leave a comment on “Something Weird”

Log In or post as a guest

Replying to comment #:

« Return to Article