• (cs)

    for (; aRange <= aValue; aValue -= aRange);

    wtf?!?!?!

  • (cs) in reply to christoofar

    Ok, I'll say it......"Brillant!"[;)]

  • III (unregistered)

    i'm getting old.

    it took me a couple of minutes to figure out how you could get an infinite loop.

    brillant indeed...

  • (cs)

    Wow. This has all the brillance of a true genus.

  • (cs) in reply to Mike R

    And on a more serious note:

    Where does this "genius" get off  calling this thing a random number generator,

    This whole train-wreck of a function could be summed as follows:

    return rand()*(iMax-iMin)+iMin

  • (cs)

    I gotta ask...

    Was her name Paula Bean?

        -dZ.

  • (cs)

    Here's a WTF:
    int aFractionPortion = rand();
    double aValue = (double)anIntegerPortion + 1.0/(double)aFractionPortion;
    From the documentation for rand():

    "The rand function returns a pseudorandom integer in the range 0 to RAND_MAX." 

    But it will only cause the occasional warp core breech, since rand doesn't return zero all that often.


  • (cs) in reply to Mike R
    Mike R:
    And on a more serious note:

    Where does this "genius" get off  calling this thing a random number generator,

    This whole train-wreck of a function could be summed as follows:

    return rand()*(iMax-iMin)+iMin



    [:$] Heh.. replace rand with (float(rand())/float(RAND_MAX))
  • (cs)

    The naming of the variables kills me.
    (a|an|the|its)

    Maybe she's a "hungarian notation fundamentalist" ducks

  • kurt (unregistered)

    The one I saw immediately was that there was no check for zero in aFractionPortion.
    // get a random double

    int anIntegerPortion = rand();
    int aFractionPortion = rand();
    double aValue = (double)anIntegerPortion + 1.0/(double)aFractionPortion;

  • Daveh (unregistered)

    I must say, that while I have made some RNG functions in my time, I never thought of doing it this way...

    1. Never seen a form of 'hungarian' notation using the/a/an before.
    2. If theMin == theMax, shouldn't you return theMin by definition?
    3. It would be convienent if GenerateRandomValue(1, 10) was equivalent to GenerateRandomValue(10, 1) (minor, but technically they mean the same).
    4. rand() returns a number from 0 to 32767, so there is a potentialy divide by zero error (assuming this is C++).
    5. The fractional portion of the generated 'random' number will not be equally distributed (there will be many more values closer to 0.0 than 1.0). For instance with a non-fractional range (say 1.0 to 10.0) its impossible to get a random number ending in 0.51 to 0.99.
    6. You can only end up getting numbers from theMin to theMin+32768. If you wanted to do a GenerateRandomValue(1, 1e6), well, you're just outta luck.
    7. I'm afraid I don't see how it can be an infinite loop...a true 'genuis' would know about fmod() or know how to write one.
    8. Love some of the comments, like for the range calculation (ohh, so that's how you compute a range from a max/min).


  • th0mas (unregistered)

    i don't see the infinite loop.  Anyone want to fill me in?

  • III (unregistered) in reply to th0mas

    infinite loop

    doubles are not real numbers - they are approximations. the math that happens with them is therefore imperfect as well.

  • Henryk Pl&#246;tz (unregistered) in reply to JimNtexas

    Moin,

    JimNtexas:
    Here's a WTF:
    int aFractionPortion = rand();
    double aValue = (double)anIntegerPortion + 1.0/(double)aFractionPortion;

    Yeah, even better: This way the fractional parts are not uniformly distributed but strongly tend to small values. E.g. you might get crash, 1, 0.5, 0.33, 0.25, 0.2 ... but never ever will this produce a number that ends in .6 for example. Talk about random ...

    -- 
    Henryk Plötz
    Grüße aus Berlin
  • ColdPie (unregistered) in reply to th0mas

    RE: Infinite loop

    What happens in the for loop if theMin==theMax? :)

  • th0mas (unregistered) in reply to ColdPie
    Anonymous:
    RE: Infinite loop

    What happens in the for loop if theMin==theMax? :)


    Then the function returns 0.0.  Read the first five lines of the WTFunction :).
  • (cs) in reply to th0mas
    Anonymous:
    i don't see the infinite loop.  Anyone want to fill me in?


    If the range is very close to zero, the subtraction in the loop may fail to change the value, I think.

    Anyone else notice this isn't a random number generator at all? rand() is the RNG. This function just gets gets a random number and then poorly tries to map it to a range. This code is impressively bad.

    I've never written an RNG myself, but I can't imagine you could do a decent one in just a few lines of code.
  • ColdPie (unregistered) in reply to th0mas
    Anonymous:
    Anonymous:
    RE: Infinite loop

    What happens in the for loop if theMin==theMax? :)


    Then the function returns 0.0.  Read the first five lines of the WTFunction :).


    Right. My mistake :)
    I guess it's just some fudgy stuff with doubles that other people have been mentioning, then?
  • (cs) in reply to ColdPie
    Anonymous:
    RE: Infinite loop

    What happens in the for loop if theMin==theMax? :)

    the function returns 0 as per the first conditional. of course. what else would you expect?

    my wtf (among all the others): why require an instance variable for this function?

  • (cs)
    Alex Papadimoulis:

    double MUTils::GenerateRandomValue(double theMin, double theMax)
    {
      if (theMin >= theMax) {
        // error in parameters, so return 0.0
        return 0.0;
      }

    // make sure the output of this function is never used as a denominator!!

    Alex Papadimoulis:
    // make the double be between 0 and aRange for (; aRange <= aValue; aValue -= aRange);

    modulo is just too damn slow!

  • th0mas (unregistered) in reply to III
    Anonymous:
    >infinite loop

    doubles are not real numbers - they are approximations. the math that happens with them is therefore imperfect as well.



    oh, okay.. I figured that this was the only possibility.  When aRange is computed as a really really small value, subtracting it may not have any effect on aValue and hence the loop is never completed.

  • Daveh (unregistered) in reply to ColdPie
    Anonymous:
    RE: Infinite loop

    What happens in the for loop if theMin==theMax? :)


    It can't as there is a check for equality in the start of the method. For very small ranges, though, it might appear as an infinite loop as it may take a long time to fix aValue to the required range (consider a range of 0.0001 when aValue is 32000).
  • mpComplete (unregistered) in reply to ColdPie

    RE: Infinite loop

    Floating point arithmetic is imprecise.  If you add two numbers of greatly differing magnitude, the smaller number will not have an effect.  For example, 1e10 + 0.001 == 1e10.  So if aRange was much smaller than aValue, aValue would never change.

  • Daveh (unregistered) in reply to mpComplete
    Anonymous:

    Floating point arithmetic is imprecise.  If you add two numbers of greatly differing magnitude, the smaller number will not have an effect.  For example, 1e10 + 0.001 == 1e10.  So if aRange was much smaller than aValue, aValue would never change.



    Ahh...I see, although this would only occur for very small ranges. If the maxium output of rand() is 32767 and we're using 64 bit doubles, you don't start seeing losses due to imprecision until your range is down around 1e-11. Of course you'd start to see significant performance issues far before this. With just a moderately small range of 1e-6 the for loop would, on average, run 16 billion times (though its a great excuse for hardware upgrades)!
  • (cs)

    no seriously, someone gimme whatever shes making and ill make hardcore functions and get drunk all day, what a life. who hires these sort of people? i need a raise

  • Ford Prefect (unregistered)

    Warn me to put on my shades before you show me code like this!!

  • (cs)

    int aFractionPortion = 0;
    double aValue = (double)anIntegerPortion + 1.0/(double)aFractionPortion;
    double aRange = theMax - theMin;
    for (; aRange <= aValue; aValue -= aRange);


    Tada! <:o)

  • (cs) in reply to Ford Prefect

    At least no controvery like yeseterday.
    I think we all can agree this is WTFery of the highest caliber.

  • Daniel (unregistered) in reply to munificent

    Are you sure the only way for an infinite loop is when aRange is very small?
    I don't know C's semantics for doubles, but isn't there some special value for infinity?
    If aValue is infinity (doesn't a division through zero, which happens in 1/32768 cases, return infinity), the loop will also never terminate independent on how big aRange is.

  • (cs) in reply to Daniel

    Indeed, depending on the FPU setup this might go into an infinite loop instead of crashing if the second rand() call returns zero.

  • Quietust (unregistered) in reply to CornedBee
    CornedBee:
    Indeed, depending on the FPU setup this might go into an infinite loop instead of crashing if the second rand() call returns zero.


    Just tested with C - apparently, a floating point division by zero will simply result in infinity, whereas an INTEGER divide by zero will throw an Integer Division by Zero exception (0xC0000094 under Windows NT) and crash the program.
  • aaron (unregistered)

    In addition to the infinite loops mentioned, there may also be a possibility of infinite loop due to very large range, as aValue-=aRange may wrap around to some really large number and you are back to the start.  I've stupidly done this myself with off-by-one-errors and integers...wondering why the loop spins and spins on while (i >= MIN_INT_VALUE) :)  (this was in Java though, but the same idea applies)

  • (cs)

    At least it's pretty well-commented.

  • (cs) in reply to munificent
    munificent:

    I've never written an RNG myself, but I can't imagine you could do a decent one in just a few lines of code.


    Depends on what you consider "decent". Obviously it's quite easy to do a lot better than this, but getting the numbers really random (or rather, pseudo-random) without a possibility of short periods or uneven distribution of randomness between bits, is quite difficult.
  • (cs) in reply to John Smallberries
    John Smallberries:
    At least no controvery like yeseterday.

    I think we all can agree this is WTFery of the highest caliber.


    I think so too. I agree that she's not the "Genius" she thinks she is. She merely seems to be an average programmer, doing most things right, failing (getting a blackout?) now-and-then. Most WTF postings here are definitely true WTF:s, making you ask: "How could he/she even get the idea of doing that?"

  • (cs) in reply to md2perpe
    md2perpe:
    John Smallberries:
    At least no controvery like yeseterday.

    I think we all can agree this is WTFery of the highest caliber.


    I think so too. I agree that she's not the "Genius" she thinks she is. She merely seems to be an average programmer, doing most things right, failing (getting a blackout?) now-and-then. Most WTF postings here are definitely true WTF:s, making you ask: "How could he/she even get the idea of doing that?"



    Are you kidding??

    This is far below what an average programmer would produce. Then again what is average? The pure lack of insight into this random number generator is unbelievable. If this were done by an average programmer, then they surely wouldn't have coughed up a hairball of a routine like this to get a random number from x to y.

  • (cs) in reply to th0mas
    Anonymous:
    Anonymous:
    RE: Infinite loop

    What happens in the for loop if theMin==theMax? :)


    Then the function returns 0.0.  Read the first five lines of the WTFunction :).


    WTFunction ... OMG!!! That is the funniest thing! Ha, ha, ha!

        -dZ.
  • (cs) in reply to Sean
    Sean:
    At least it's pretty well-commented.


    Yes, the comments are pretty.

        -dZ.
  • (cs) in reply to Mike R

    This was probably written by a "my random number generator is better than YOUR random number generator," only if you're looking for a random number generator that doesn't randomly produce numbers that appear to be random.

  • The Frog (unregistered)

    My initial thought:
        Why didn't the "Genius" do what the rest of us would do, Google "Random Number C++ Double"

    My second thought:
        Crap, what "Brillant" Genius is posting thier random number generators.

    My next thought:
        OMG, OMG, OMG

  • (cs) in reply to chb

    Yeah I never got why some people think 'theNumber' is superior to 'number' or 'itsValue' is better than 'value'.  The place that produced this was rife with that sort of needless (and useless) decoration.

  • (cs)

    You know, it might be time to make a serious effort to start getting "Brillant" entered in the wikis out there (encyclopedias, dictionaries - is there one?)

    Then the rest of the world would understand us when we use it in reference to stuff like this.

    And it makes such a great pejorative, we should share it with the world!

  • (cs) in reply to aaron
    Anonymous:
    In addition to the infinite loops mentioned, there may also be a possibility of infinite loop due to *very large range*, as aValue-=aRange may wrap around to some really large number and you are back to the start.  I've stupidly done this myself with off-by-one-errors and integers...wondering why the loop spins and spins on while (i >= MIN_INT_VALUE) :)  (this was in Java though, but the same idea applies)


    Wouldn't the constraint on the for loop remove this case?
      // make the double be between 0 and aRange
    for (; aRange <= aValue; aValue -= aRange);
    if aRange is very large, wouldn't aValue be less than it?  Since aValue will be between 0 and 32768 (unless it's infinity...in which case the value of aRange doesn't matter).

    -ds
  • (cs)

    itsSeed also appears to be a global... shame shame!

  • (cs)

    It seems to me that anytime the range is mostly negative, the routine will enter an endless loop condition.

    Example: <FONT face="Courier New">theMin := -15  theMax:=0</FONT>

    Also, it might just SEEM like an endless loop if <FONT face="Courier New">theMin</FONT> is a very large number, such as 3.1E31

    (this is my first WTF post -- thanks to Dennis Sherman for telling me about this site)

  • (cs)

    Did the word "MENSA" pepper her speech?

  • Robert (unregistered) in reply to christoofar

     > year or so after she left

    Women and Computer Science don't mix! ;-)

  • (cs) in reply to chb
    chb:
    The naming of the variables kills me.
    (a|an|the|its)

    Maybe she's a "hungarian notation fundamentalist" *ducks*


    I was going to jokingly make this comment, but figured I'd better not after yesterdays post.
  • database guy (unregistered) in reply to Mike R

    Thank you, I don't know why people are so terrified of a using little arithmetic now and then.

  • database guy (unregistered) in reply to Mike R

    Mike R:
    Mike R:
    And on a more serious note:

    Where does this "genius" get off  calling this thing a random number generator,

    This whole train-wreck of a function could be summed as follows:

    return rand()*(iMax-iMin)+iMin



    [:$] Heh.. replace rand with (float(rand())/float(RAND_MAX))

    Thank you, I don't know why people are so terrified of a using little arithmetic now and then.

Leave a comment on “Seriously, I'm A Genius”

Log In or post as a guest

Replying to comment #:

« Return to Article