• diaphanein (unregistered) in reply to res2

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

    itsSeed is a member field.  Here's your clue:  this->itsSeed = time(NULL);

  • GogglesPisano (unregistered) in reply to th0mas

    This won't work for very small or very large values.  Also, I don't believe it will work as expected for negative values.

  • (cs) in reply to ammoQ

    The "<FONT face="Courier New">if (theMin >= theMax)</FONT>" check will fail to prevent negative values as long as this condition isn't true, so negative parameter values can still create an infinite loop condition.

    The anonymous poster used incorrect parameter values for their example.  They should have used theMin:=-15 and theMax:=0.

  • (cs) in reply to Robert
    Anonymous:

     > year or so after she left

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



    I've noticed that females involved in Software Engineering seem to be better at QA and understanding business requirements than many men.  Dont most business decisions and requirements mimic the same reasoning behind impulse buys and lust for designer shoes (without the sticker price)?
  • (cs) in reply to christoofar
    christoofar:
    Anonymous:

     > year or so after she left

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



    I've noticed that females involved in Software Engineering seem to be better at QA and understanding business requirements than many men.  Dont most business decisions and requirements mimic the same reasoning behind impulse buys and lust for designer shoes (without the sticker price)?

    I've known many men who impulsively made decisions to go with the latest "designer" software...and those had one hell of a sticker price.
  • (cs) in reply to md2perpe
    md2perpe:
    Anonymous:

     > year or so after she left

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


    The world's first programmer: http://en.wikipedia.org/wiki/Ada_Lovelace

    While I disagree with the women and CS don't mix thing, it's pretty clear from historical records that Ada Lovelace never did anything but write some papers on how great Babbages machine would be when complete.  It's not clear that she had any meaningful understanding of the machine.  In short, she is famous for saying 'seriously, I'm actually a genius.'

  • (cs) in reply to aikimark
    aikimark:

    The "<font face="Courier New">if (theMin >= theMax)</font>" check will fail to prevent negative values as long as this condition isn't true, so negative parameter values can still create an infinite loop condition.

    The anonymous poster used incorrect parameter values for their example.  They should have used theMin:=-15 and theMax:=0.



    aRange = theMax - theMin;

    =

    aRange = 0 - (-15);

    =

    aRange = 15;

    sorry, no infinite loop this time ;-)


  • CJ (unregistered) in reply to Robert

    > > year or so after she left

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


    Hey! :@

    (Admittedly, I can't quite this preview thing to work, but I doubt that it's because I'm a woman.  Seriously, this comment form sucks)
  • (cs) in reply to diaphanein

    itsSeed is a member field. Here's your clue: this->itsSeed = time(NULL);

    i'm pretty sure static class members (aka globals) can be referenced via "this->". however, the naming convention leads me to believe that it's an instance variable.

    the wtf about that is that an instance variable is being used to keep track of whether a global (i.e. the prng seed) has been initialized.

  • Brandon (unregistered) in reply to DZ-Jay

    Perhaps adding an entry to: http://brilliant.urbanup.com

     

  • (cs) in reply to dubwai

    dubwai:
    While I disagree with the women and CS don't mix thing, it's pretty clear from historical records that Ada Lovelace never did anything but write some papers on how great Babbages machine would be when complete.  It's not clear that she had any meaningful understanding of the machine.  In short, she is famous for saying 'seriously, I'm actually a genius.'

    <FONT face="Courier New" size=2>having gone to great lengths to obtain a copy of an out of print biography on ada lovelace [0], i'll say there is plenty of evidence to suggest she was quite capable of understanding the machine.  you have to consider the times, as well.  being a self-taught mathematical hobbyist wasn't a common thing for women to take up back then - that she did pursue that route, and made interesting observations about algorithms ahead of church-turing's time, says a lot.  her life towards the end was kind of depressing: she became addicted to morphine after using it to treat an illness, which probably didn't do much for her abilities.</FONT>

    <FONT face="Courier New" size=2>[0] http://www.amazon.com/exec/obidos/ASIN/0875185983/thegreatideafind/104-3701586-1679962</FONT>

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


    This is a general problem.  "Number" and "Value" are pretty useless variable names to begin with.  I ran across a variable today called "myThis", which combines two of my favourite naming WTFs in one: "my" and "this"!  Both of these are, of course, as semantically bankrupt as "Number" and "Value", and putting them together doesn't help one bit.  Might as well call it "yourThat" for all the good the variable name does me as I read the code.

    (I've never really liked using "this" to mean "current object", any more than I like Microsoft's "my" naming convention for desktop entities.  Both are more of a hindrance to clear and logical thought than a help, particularly in conversations, which start to resemble Who's On First pretty quickly.)

    Proper variable naming is at least as important as proper commenting, and for the same reason: the compiler doesn't care about either one, but people reading the code sure do!  A misnamed variable is just as bad as a misleading comment.  Neither is worthy of the label "professional".

  • (cs) in reply to emptyset
    emptyset:

    dubwai:
    While I disagree with the women and CS don't mix thing, it's pretty clear from historical records that Ada Lovelace never did anything but write some papers on how great Babbages machine would be when complete.  It's not clear that she had any meaningful understanding of the machine.  In short, she is famous for saying 'seriously, I'm actually a genius.'

    <FONT face="Courier New" size=2>having gone to great lengths to obtain a copy of an out of print biography on ada lovelace [0], i'll say there is plenty of evidence to suggest she was quite capable of understanding the machine.  you have to consider the times, as well.  being a self-taught mathematical hobbyist wasn't a common thing for women to take up back then - that she did pursue that route, and made interesting observations about algorithms ahead of church-turing's time, says a lot.  her life towards the end was kind of depressing: she became addicted to morphine after using it to treat an illness, which probably didn't do much for her abilities.</FONT>

    <FONT face="Courier New" size=2>[0] http://www.amazon.com/exec/obidos/ASIN/0875185983/thegreatideafind/104-3701586-1679962</FONT>

    I basing my statements on a book written by a researcher who was involved with completing a working reproduction of the never finished machine (they had to make some changes to the design to make it actually do anything.)  What he found from her writings and Babbage's was that Ada Lovelace had produced next to nothing towards the project.

    If you have a links to reproductions of her work, I'd be open to other views.  All I can find is that she was able to imagine the uses of computers way before her time.  This makes her a great anticipator but not a programmer.

    In all reality, though, the first programmers were women.  'Computer' used to be a job description.  It's what female mathematicians did before the programmable computer.  When ENIAC was created, the programmers were the people who were most familiar with the alogrithms: the female computers.

  • (cs) in reply to stevekj
    stevekj:


    This is a general problem.  "Number" and "Value" are pretty useless variable names to begin with.  I ran across a variable today called "myThis", which combines two of my favourite naming WTFs in one: "my" and "this"!  Both of these are, of course, as semantically bankrupt as "Number" and "Value", and putting them together doesn't help one bit.  Might as well call it "yourThat" for all the good the variable name does me as I read the code.

    (I've never really liked using "this" to mean "current object", any more than I like Microsoft's "my" naming convention for desktop entities.  Both are more of a hindrance to clear and logical thought than a help, particularly in conversations, which start to resemble Who's On First pretty quickly.)

    Proper variable naming is at least as important as proper commenting, and for the same reason: the compiler doesn't care about either one, but people reading the code sure do!  A misnamed variable is just as bad as a misleading comment.  Neither is worthy of the label "professional".



    Prefixes like "my" are an easy solution for a conflict with reserved names, especially when it comes to variable names like "value" or "number". Today I tried to compile a very old program, which had a function "pow" that conflicted with a built-in function of the compiler; so I changed it to "mypow".
  • (cs) in reply to ammoQ
    Today I tried to compile a very old program, which had a function "pow" that conflicted with a built-in function of the compiler; so I changed it to "mypow".


    When given the choice between a 'semantically bankrupt' (must remember that one) and a verbose name, I opt for the latter.

    I'd have named the function 'raiseToPower' or 'raisePow'. 'mypow' immediately causes me to think oh you can do better than that.
  • (cs) in reply to dhromed

    *when I'm seriously working, of course. If I need to quickly sketch a snippet, I'd probably have called it 'mypow'. Or 'powpow'. Or 'poof'.

  • (cs) in reply to dhromed
    dhromed:
    Today I tried to compile a very old program, which had a function "pow" that conflicted with a built-in function of the compiler; so I changed it to "mypow".


    When given the choice between a 'semantically bankrupt' (must remember that one) and a verbose name, I opt for the latter.

    I'd have named the function 'raiseToPower' or 'raisePow'. 'mypow' immediately causes me to think oh you can do better than that.


    Actually it was just curious if the compile (freepascal) was able to compile the program (Turbo pascal). I'm not even sure if the "pow" function in this program really meant "raiseToPower".
    Even if so, if that was a serious program, I would have had to check if it makes sense to take it out and use the built-in function. Anyway, the prefix "my" is a good indicator for "likely reinvented the wheel".
  • (cs) in reply to stevekj
    stevekj:

    This is a general problem.  "Number" and "Value" are pretty useless variable names to begin with.


    Well, that depends on the context. For example, in the implementation of a hash table (or any kind of lookup functionality),
    "value" has a well-defined technical meaning.
  • (cs) in reply to ammoQ

    The hungarian version of that is "asjf_".

  • (cs) in reply to dhromed

    Drat, threads knit fast around here.

    My previous post in reply to:

    Anyway, the prefix "my" is a good indicator for "likely reinvented the wheel".
  • diaphanein (unregistered) in reply to III

    III:
    >itsSeed is a member field. Here's your clue: this->itsSeed = time(NULL); i'm pretty sure static class members (aka globals) can be referenced via "this->". however, the naming convention leads me to believe that it's an instance variable. the wtf about that is that an instance variable is being used to keep track of whether a global (i.e. the prng seed) has been initialized.

    I'll be damned.  You're right.  The folowing compiles without warnings or errors in VC7.  Certainly doesn't feel right, though...statics don't belong to the instance...I hate C++.  I really do.

    <FONT color=#0000ff size=1>

    #include</FONT><FONT size=1> </FONT><FONT color=#808000 size=1><IOSTREAM>
    </FONT><FONT color=#0000ff size=1>struct</FONT><FONT size=1> StaticTest
    {
    </FONT><FONT color=#0000ff size=1>static</FONT><FONT size=1> </FONT><FONT color=#0000ff size=1>long</FONT><FONT size=1> field;
    </FONT><FONT color=#0000ff size=1>void</FONT><FONT size=1> printField()
    {
    std::cout << </FONT><FONT color=#0000ff size=1>this</FONT><FONT size=1>->field << std::endl;
    }
    };

    </FONT><FONT color=#0000ff size=1>

    long</FONT><FONT size=1> StaticTest::field = 1;</FONT>

    <FONT color=#0000ff size=1>int</FONT><FONT size=1> _tmain(</FONT><FONT color=#0000ff size=1>int</FONT><FONT size=1> argc, const char** argv )
    {
    StaticTest test;
    test.printField();
    </FONT><FONT color=#0000ff size=1>return</FONT><FONT size=1> 0;
    }

    </FONT>
  • (cs) in reply to diaphanein

    IIRC java allows that, too.

  • Richard P (unregistered) in reply to EsotericMoniker
    EsotericMoniker:
    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.


    "number" and "value" might be keywords.  Easier just to add letters rather than RTFM or hit Compile.

    More likely, the author is saying all the statements in his or her mind as she writes them and finds something closer to proper english easier to work with.
  • (cs) in reply to christoofar
    christoofar:
    Anonymous:

     > year or so after she left

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



    I've noticed that females involved in Software Engineering seem to be better at QA and understanding business requirements than many men.  Dont most business decisions and requirements mimic the same reasoning behind impulse buys and lust for designer shoes (without the sticker price)?


    And men wonder why women in technical fields say they have to deal with a ton of sexism.
  • (cs) in reply to dhromed
    dhromed:
    Today I tried to compile a very old program, which had a function "pow" that conflicted with a built-in function of the compiler; so I changed it to "mypow".


    When given the choice between a 'semantically bankrupt' (must remember that one) and a verbose name, I opt for the latter.

    I'd have named the function 'raiseToPower' or 'raisePow'. 'mypow' immediately causes me to think oh you can do better than that.


    Namespace means nothing.

    I would have figured out what the function did, determined if it was developed by NIH syndrom, if so, use the already invented (and likely adequately debugged function) and dumped the dupe. Or, if It didn't suffer from NIH, and was a novel function, I would have given it a more descriptive name like:

    MakePowSound, PreviousOctalWord, PushOtherWorld, PressOrangeWidget, etc.. (note these names depend on functionality)

    or if my "Function to raise a number to a power" were a superefficient über-function, then I'd use hungarian notation and name it ufihPow.

  • (cs) in reply to ammoQ
    ammoQ:
    IIRC java allows that, too.


    If you're talking about static fields being accessible through an instance, yes.

    The unanimous opinion of comp.lang.java.programmer is that it's one of the language designers' biggest mistakes.
  • Vicky (unregistered) in reply to mizhi
    mizhi:
    christoofar:
    Anonymous:

     > year or so after she left

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



    I've noticed that females involved in Software Engineering seem to be better at QA and understanding business requirements than many men.  Dont most business decisions and requirements mimic the same reasoning behind impulse buys and lust for designer shoes (without the sticker price)?


    And men wonder why women in technical fields say they have to deal with a ton of sexism.


    Wordy McFucking Word, as one of my good friends would say.

    Personally, I've not experienced too much in the way of ignorant sexism in my professional life. Online, in fora like this one, is a whole different kettle of fish. It's quite pathetic and it's one of the reasons I've not posted here before, even though I've been a regular reader for a while.

    Vicky
  • (cs) in reply to Vicky
    Anonymous:
    mizhi:

    And men wonder why women in technical fields say they have to deal with a ton of sexism.


    Wordy McFucking Word, as one of my good friends would say.

    Personally, I've not experienced too much in the way of ignorant sexism in my professional life. Online, in fora like this one, is a whole different kettle of fish. It's quite pathetic and it's one of the reasons I've not posted here before, even though I've been a regular reader for a while.


    Anonymity gives people the balls to say things they would not otherwise say aloud.
  • (cs) in reply to Vicky

    Anonymous:

    Wordy McFucking Word, as one of my good friends would say.

    <FONT face="Courier New" size=2>are you from the midwest?</FONT>

    Anonymous:


    Personally, I've not experienced too much in the way of ignorant sexism in my professional life. Online, in fora like this one, is a whole different kettle of fish. It's quite pathetic and it's one of the reasons I've not posted here before, even though I've been a regular reader for a while.

    <FONT face="Courier New" size=2>that's good news.  at my old workplace, i was told (by a female coworker) that women never got promoted.  things like that can occur, but most of the harsh rules in place are a deterrent towards sexist attitudes.  ah, that reminds me (classic):</FONT>

    <FONT face="Courier New" size=2>Ian: Ah, shit. They are not gonna release the album...because they have decided that the cover is sexist.
    Nigel:  Well so what?  What's wrong with being sexy? I mean there's no....
    Ian:    Sex-ist.
    David:  -ist, not sexy.</FONT>

  • (cs) in reply to mizhi
    mizhi:
    Anonymous:
    mizhi:

    And men wonder why women in technical fields say they have to deal with a ton of sexism.


    Wordy McFucking Word, as one of my good friends would say.

    Personally, I've not experienced too much in the way of ignorant sexism in my professional life. Online, in fora like this one, is a whole different kettle of fish. It's quite pathetic and it's one of the reasons I've not posted here before, even though I've been a regular reader for a while.


    Anonymity gives people the balls to say things they would not otherwise say aloud.


    Shrug. My main reason for not registering before was lack of time, rather than a desire for anonymity, but I've registered now, does that help? I'm not putting my email address up in my public profile if that's what you're getting at: I've done that before and ended up receiving all sorts of delightful things in my inbox.

    Vicky

  • (cs) in reply to emptyset
    emptyset:

    Anonymous:

    Wordy McFucking Word, as one of my good friends would say.

    <font face="Courier New" size="2">are you from the midwest?
    </font>

    <font face="Courier New" size="2">


    </font>

    <font face="Courier New" size="2">Nope, I'm in the UK. And my friend who says that a lot is Canadian!
    </font>

    Vicky

  • (cs) in reply to Vicky
    Vicky:
    mizhi:
    Anonymous:
    mizhi:

    And men wonder why women in technical fields say they have to deal with a ton of sexism.


    Wordy McFucking Word, as one of my good friends would say.

    Personally, I've not experienced too much in the way of ignorant sexism in my professional life. Online, in fora like this one, is a whole different kettle of fish. It's quite pathetic and it's one of the reasons I've not posted here before, even though I've been a regular reader for a while.


    Anonymity gives people the balls to say things they would not otherwise say aloud.


    Shrug. My main reason for not registering before was lack of time, rather than a desire for anonymity, but I've registered now, does that help? I'm not putting my email address up in my public profile if that's what you're getting at: I've done that before and ended up receiving all sorts of delightful things in my inbox.

    Vicky



    OK, I just realised that you might have been referring to the people who made the ignorant remarks in the first place, rather than me. If that's the case I apologise for snapping!

    Vicky

  • (cs) in reply to Vicky
    Vicky:

    Shrug. My main reason for not registering before was lack of time, rather than a desire for anonymity, but I've registered now, does that help? I'm not putting my email address up in my public profile if that's what you're getting at: I've done that before and ended up receiving all sorts of delightful things in my inbox.


    Actually, I wasn't referring specifically to you.  To people on the net in general.  Even if a person is "registered" on a website, they might as well be a talent sea otter for all one can verify. [:)]
  • (cs) in reply to Vicky
    Vicky:

    OK, I just realised that you might have been referring to the people who made the ignorant remarks in the first place, rather than me. If that's the case I apologise for snapping!


    I've had more caustic things said to me over a misplaced period.  No worries. [:)]
  • (cs)

    Hi, this is the submitter of this WTF.

    Just felt I'd clear up a few things:

    1.  You can't blame the naming convention (its/a/an/the) on The Genius, it was a naming convention long established by our organization and now followed for consistency.  It's not the worst thing in the world, and, as others have pointed out, it does conveniently prevent a lot of name overloading problems with libraries & such.

    2.  We were mistaken about the crash opportunity - we assumed division by zero would result in an exception, but it does return Inf, even on Solaris.  As if that were the least of the problems in this function.

    3.  In scanning the posts, it seems like everyone here has hit the high points of this particular function.  I have to admit that the "reciprocal technique" of generating a random double was always my favorite part.

    4.  I've always compared this function to the quote by some famous sculptor (sorry, can't remember who) about how to carve an elephant:  "Start with a rock, then chip away everything that doesn't look like an elephant."

    4.  Yes, she was a Mensa member.

    -BbT

  • (cs) in reply to BigBoote66

    One more thing - her name wasn't Paula Bean.

    -BbT

  • (cs) in reply to BigBoote66
    BigBoote66:
    Hi, this is the submitter of this WTF.

    Just felt I'd clear up a few things:


    Sure, this late in the game clarification is everything


    1.  You can't blame the naming convention (its/a/an/the) on The Genius, it was a naming convention long established by our organization and now followed for consistency.  It's not the worst thing in the world, and, as others have pointed out, it does conveniently prevent a lot of name overloading problems with libraries & such.


    If I were to work at an organisation that insisted upon unruly and useless warts as a naming convention, my resume would be posted everywhere. Hell, I'd even consider relocating to Elbonia. I can understand standardising on prefixing the name with its type, I can understand prefixing it with its scope, I can understand prefixing it like microsoft does in it's SDK, but I simply cannot fathom the stupidity of prefixing everything with the and its. itsIdiotic.


    2.  We were mistaken about the crash opportunity - we assumed division by zero would result in an exception, but it does return Inf, even on Solaris.  As if that were the least of the problems in this function.


    Depends on whether the hardware has that exception masked out (Floating-point divide by zero) it can be enabled, and at that point will be a crash opportunity. IIRC, there's a way to turn the exception on and off in the C standard library.


    3.  In scanning the posts, it seems like everyone here has hit the high points of this particular function.  I have to admit that the "reciprocal technique" of generating a random double was always my favorite part.


    Yep, when so many other techniques are totally obvious.


    4.  I've always compared this function to the quote by some famous sculptor (sorry, can't remember who) about how to carve an elephant:  "Start with a rock, then chip away everything that doesn't look like an elephant."


    More like throw spaghetti against the wall to see what sticks... seriously.


    4.  Yes, she was a Mensa member.


    Good reason to never join Mensa.
  • (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

    No, that would be better than the function written because it would actually stand a chance of having an even distribution.  This function doesn't have a prayer.

  • vDave420 (unregistered) in reply to R. Armiento
    Anonymous:
    This truely is a random function!, when run, it does something completely random; it either crash (divide by 0) [...]


    No, it should ( C ) throw the double to an +inf value, from which the infinite loop would occur, since (+Inf - N --> +Inf) IIRC for double & floats.

    I expect this was the source of the 100% CPU, and not a very very very small range.

             -dave-
  • (cs) in reply to Mike R
    Mike R:
    If I were to work at an organisation that insisted upon unruly and useless warts as a naming convention, my resume would be posted everywhere. Hell, I'd even consider relocating to Elbonia. I can understand standardising on prefixing the name with its type, I can understand prefixing it with its scope, I can understand prefixing it like microsoft does in it's SDK, but I simply cannot fathom the stupidity of prefixing everything with the and its. itsIdiotic.
    You probably missed some of the commentary above - the naming convention is scope based:

    a/an - local variable
    the - parameter
    its - member data

    I'm not sure what our convention is for globals, as I've never seen them in the code.  The convention itself was inherited from the commercial product XVT (a cross-platform GUI library), which seemed as good as any to use by the original designers of the system, as it used XVT for many of its clients.

    I can only dream of the programmer's paradise you must live in if following an odd but non-horrible naming convention is sufficient to drive you to looking for other employment.

    -BbT

  • (cs) in reply to BigBoote66
    BigBoote66:
    I'm not sure what our convention is for globals


    TheOneAndOnly
  • Anonymous (unregistered) in reply to Xepol
    Xepol:
    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!


    Actually it means "bright" or "shining" in French.
  • (cs) in reply to Mike R


    BigBoote66:

    4.  Yes, she was a Mensa member.


    Good reason to never join Mensa.


    Well, anyone who's studied high-IQ societies at all will immediately realize a few things:

    1) IQ is difficult to measure accurately, sort of like programmer productivity - take it with a fair amount of salt;

    2) Mensa contains a disproportionate number of people who like to brag about their intelligence, but who don't necessarily actually have a whole lot of it (at a 98th percentile cutoff, it means that if you can get through high school without cheating, you can probably join); and

    3) There are lots of high-IQ societies above Mensa, most of which contain much lower proportions of braggarts.

    I believe it was Groucho Marx who said "I would never join a club that would have me as a member".
  • Using simple arithmetic... (unregistered) in reply to database guy
    Anonymous:

    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.



    It seems that neither of the above posters used a little arithmetic either :)

    That function doesn't even come close to generating a value from iMin to iMax.  If you had iMin = 2 and iMax = 10 you'd be producing something from 2 to (RAND_MAX * 8), which is nothing close to a value between 2 and 8!


    The real WTF here is how many people like to dump on other people when they themselves are clueless!


    If you wanted to produce a psuedo-random value from A - B you'd do something like:

    value = rand()% (iMax - iMin) + iMin;   // The simple way that most average programmers would use.


    Or better (to use the avoid only masking on the lower bits of the number generated):


    double value = (double) rand()/ (double) ( (RAND_MAX + 1)/iMax ) ;

  • (cs) in reply to Using simple arithmetic...
    Anonymous:
    Anonymous:

    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.



    It seems that neither of the above posters used a little arithmetic either :)

    That function doesn't even come close to generating a value from iMin to iMax.  If you had iMin = 2 and iMax = 10 you'd be producing something from 2 to (RAND_MAX * 8), which is nothing close to a value between 2 and 8!


    The real WTF here is how many people like to dump on other people when they themselves are clueless!


    If you wanted to produce a psuedo-random value from A - B you'd do something like:

    value = rand()% (iMax - iMin) + iMin;   // The simple way that most average programmers would use.


    Or better (to use the avoid only masking on the lower bits of the number generated):


    double value = (double) rand()/ (double) ( (RAND_MAX + 1)/iMax ) ;



    Are you dumb or just stupid?

    The real WTF here is that you're even more clueless.

    Here is my code, which I posted as an example. Quoted above. Please notice I said the rand() call needed to be replaced with a bit of code to normalise the number to the range from 0 to 1. I was hasty in my first reply and realised the mistake, and since the forum doesn't allow editing, I simply replied with the correction in the post.



    int iMin = 5;
    int iMax = 10;

    float GetRand()
    {
        return  (float(rand())/float(RAND_MAX))*(iMax-iMin)+iMin;
    }

    int main()
    {
        printf("A listing of random numbers from %i through %i:\n\n", iMin, iMax);
        for(int n = 0; n < 100; n++)
        {
            printf("GetRand returns %f\n", GetRand());
        }
        return 0;
    }


    which outputs:


    A listing of random numbers from 5 through 10:

    GetRand returns 5.006256
    GetRand returns 7.817927
    GetRand returns 5.966521
    GetRand returns 9.043703
    GetRand returns 7.925047
    GetRand returns 7.399365
    GetRand returns 6.751457
    GetRand returns 9.479812
    GetRand returns 9.114200
    GetRand returns 8.733024
    GetRand returns 5.870540
    GetRand returns 9.294717
    GetRand returns 8.552507
    GetRand returns 7.567675
    GetRand returns 6.519974
    GetRand returns 5.074923
    GetRand returns 5.457015
    GetRand returns 6.822260
    GetRand returns 5.736564
    GetRand returns 5.829493
    GetRand returns 9.942625
    GetRand returns 7.228462
    GetRand returns 5.595416
    GetRand returns 5.023347
    GetRand returns 5.044557
    GetRand returns 6.889401
    GetRand returns 7.658315
    GetRand returns 7.855922
    GetRand returns 8.008820
    GetRand returns 8.035829
    GetRand returns 5.831172
    GetRand returns 8.315226
    GetRand returns 7.253945
    GetRand returns 6.760613
    GetRand returns 5.285195
    GetRand returns 8.038423
    GetRand returns 8.916593
    GetRand returns 9.013031
    GetRand returns 7.599414
    GetRand returns 6.509751
    GetRand returns 9.379864
    GetRand returns 8.633381
    GetRand returns 9.779504
    GetRand returns 9.628590
    GetRand returns 7.696768
    GetRand returns 5.711692
    GetRand returns 7.310404
    GetRand returns 6.176641
    GetRand returns 9.311197
    GetRand returns 6.048006
    GetRand returns 8.898282
    GetRand returns 9.218268
    GetRand returns 9.983978
    GetRand returns 9.998474
    GetRand returns 8.057497
    GetRand returns 6.962188
    GetRand returns 6.331065
    GetRand returns 6.486404
    GetRand returns 9.200720
    GetRand returns 5.118717
    GetRand returns 6.879330
    GetRand returns 5.463118
    GetRand returns 8.386029
    GetRand returns 5.281075
    GetRand returns 5.043947
    GetRand returns 9.593951
    GetRand returns 6.379437
    GetRand returns 6.364483
    GetRand returns 7.939543
    GetRand returns 8.455916
    GetRand returns 9.188055
    GetRand returns 8.632466
    GetRand returns 7.424696
    GetRand returns 6.026795
    GetRand returns 8.718680
    GetRand returns 7.342296
    GetRand returns 7.289804
    GetRand returns 9.745781
    GetRand returns 8.722190
    GetRand returns 5.541398
    GetRand returns 7.995239
    GetRand returns 6.926176
    GetRand returns 8.675039
    GetRand returns 8.044832
    GetRand returns 7.862026
    GetRand returns 6.806696
    GetRand returns 5.757775
    GetRand returns 6.125523
    GetRand returns 7.125767
    GetRand returns 9.014405
    GetRand returns 7.585528
    GetRand returns 9.949950
    GetRand returns 8.757744
    GetRand returns 6.727805
    GetRand returns 5.844905
    GetRand returns 8.286538
    GetRand returns 7.459487
    GetRand returns 5.317698
    GetRand returns 8.498795
    GetRand returns 7.524033
    Press any key to continue


    Hmm.... Numbers from 5-10. Gee, Who'da thunk.


    A listing of random numbers from 5 through 10:

    GetRand returns 0.012515
    GetRand returns 5.637057
    GetRand returns 1.933455
    GetRand returns 8.089133
    GetRand returns 5.851343
    GetRand returns 4.799756
    GetRand returns 3.503663
    GetRand returns 8.961538
    GetRand returns 8.230159
    GetRand returns 7.467643
    GetRand returns 1.741453
    GetRand returns 8.591270
    GetRand returns 7.106532
    GetRand returns 5.136447
    GetRand returns 3.040598
    GetRand returns 0.149878
    GetRand returns 0.914225
    GetRand returns 3.645299
    GetRand returns 1.473443
    GetRand returns 1.659341
    GetRand returns 9.887363
    GetRand returns 4.457875
    GetRand returns 1.191087
    GetRand returns 0.046703
    GetRand returns 0.089133
    GetRand returns 3.779609
    GetRand returns 5.317766
    GetRand returns 5.713065
    GetRand returns 6.018926
    GetRand returns 6.072955
    GetRand returns 1.662698
    GetRand returns 6.631868
    GetRand returns 4.508852
    GetRand returns 3.521978
    GetRand returns 0.570513
    GetRand returns 6.078144
    GetRand returns 7.834860
    GetRand returns 8.027778
    GetRand returns 5.199939
    GetRand returns 3.020147
    GetRand returns 8.761600
    GetRand returns 7.268315
    GetRand returns 9.561050
    GetRand returns 9.259158
    GetRand returns 5.394689
    GetRand returns 1.423687
    GetRand returns 4.621795
    GetRand returns 2.353785
    GetRand returns 8.624237
    GetRand returns 2.096459
    GetRand returns 7.798230
    GetRand returns 8.438339
    GetRand returns 9.970085
    GetRand returns 9.999084
    GetRand returns 6.116300
    GetRand returns 3.925214
    GetRand returns 2.662698
    GetRand returns 2.973443
    GetRand returns 8.403236
    GetRand returns 0.237485
    GetRand returns 3.759463
    GetRand returns 0.926435
    GetRand returns 6.773504
    GetRand returns 0.562271
    GetRand returns 0.087912
    GetRand returns 9.189866
    GetRand returns 2.759463
    GetRand returns 2.729548
    GetRand returns 5.880342
    GetRand returns 6.913309
    GetRand returns 8.377900
    GetRand returns 7.266484
    GetRand returns 4.850427
    GetRand returns 2.054029
    GetRand returns 7.438950
    GetRand returns 4.685592
    GetRand returns 4.580586
    GetRand returns 9.493590
    GetRand returns 7.445971
    GetRand returns 1.083028
    GetRand returns 5.991758
    GetRand returns 3.853175
    GetRand returns 7.351648
    GetRand returns 6.090965
    GetRand returns 5.725275
    GetRand returns 3.614164
    GetRand returns 1.515873
    GetRand returns 2.251526
    GetRand returns 4.252442
    GetRand returns 8.030525
    GetRand returns 5.172161
    GetRand returns 9.902015
    GetRand returns 7.517094
    GetRand returns 3.456349
    GetRand returns 1.690171
    GetRand returns 6.574481
    GetRand returns 4.920024
    GetRand returns 0.635531
    GetRand returns 6.999084
    GetRand returns 5.049145
    Press any key to continue


    Gee, your second function is a stellar example of fitting the specs. Returns numbers from 0 through 10.

    Mike "The real WTF here is bagging on someone for being clueless, when you yourself are clueless" R

  • Michael J (unregistered)

    Th code is fairly bad, but I don't think that the infinite loop comes from
    where most people are expecting it.

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

    In floating point maths, there is a concept known as Epsilon (e); such that
    epsilon is the smallestnumber for which x+e != x.(I hope the Epsilon comes out right - it is a greek "e" and looks like a squiggly,
    back to front 3)

    Now for a 65-bit IEEE double, epsilon is (very) approximately x/1.0e16

    i.e. for "aValue -= aRange" to have no effect on aValue, aRange must be
    less than aValue/1.0e16. Therefore, aRange must be less than aValue, so
    the for loop will exit immediately.
    So how do we get the infinite loop?

    I think it comes from the code:
    double aValue = (double)anIntegerPortion + 1.0/(double)aFractionPortion;

    In IEEE floating point maths, division by zero doesn't (normally) crash
    the program. It returns some sort of pseudo-number like NaN (Not a Number).

    NaN minus anything (or plus, times or divided by anything) = NaN.
    Also, x<NaN is always false (as is x==NaN, x>NaN etc).

    So I think our hero has struk the 1 in 32000 case of FractionPortion==0,
    thus generating a NaN and hence an infinite loop.
    Isn't FP maths fun? :)

  • Pasa (unregistered) in reply to Using simple arithmetic...

    Anonymous:

    It seems that neither of the above posters used a little arithmetic either :)
    ...
    double value = (double) rand()/ (double) ( (RAND_MAX + 1)/iMax ) ;

    Well, sure, just guess what random numbers you'll get with that refined implementation on implementations where RAND_MAX == INT_MAX.

     

  • CalliArcale (unregistered) in reply to mizhi
    mizhi:

    And men wonder why women in technical fields say they have to deal with a ton of sexism.


    Oddly, I haven't had to deal with sexism.  I did have one interview where they warned me that many of the clients wouldn't think I was capable (car dealerships), but I didn't take the job -- the offered salary sucked.  At my current job (gov't contractor) women get promoted at the same rate as men, and while men do outnumber us ladies, we are treated uniformly.

    And I can attest that at least here, women perform uniformly as well.  I've had the pleasure of working with some very dedicated, talented, and experienced female engineers, none of whom would want anything to do with this monstrous piece of code -- except to perhaps mount it on the wall of their cubicles to laugh at.
  • Michael J (unregistered) in reply to CalliArcale

    CalliArcale  wrote:
    >      mizhi wrote:

          >    And men wonder why women in technical fields say they have to deal with a ton of sexism.
     
        Oddly, I haven't had to deal with sexism.

    It has always struck me as self-destructive to accept or reject somebody on sex/race/religious grounds.
    In a previous life, I was responsible for hiring programmers.  I always figured that if I didn't hire the
    best person, I was short-changing my ability to compete in the market.  The competent person who
    I just rejected would probably go and work for the opposition, and I'd be left with a second-rate
    replacement.


    Other than ability and potential, the only other criteria I considered was "would they fit into the team?".
    I occasionally rejected somebody who's attitude was bad and who I thought would disrupt the team.

    I repeatedly hear stories of employers who won't hire women, or people of a particular race or religion.
    I've even been on the wrong end of that.  Last time I was job hunting, I spoke to a number of people
    who wouldn't consider me as I was too old (forty-something).

    Such is life. 
  • Duane (unregistered)

    If min and max are < 0 and min < max then the for loop never terminates because:

    counter -= min

    but min is negative, so counter gets larger and will never be "less than" anything.

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

Log In or post as a guest

Replying to comment #:

« Return to Article