Comment On Seriously, I'm A Genius

Steve B was kind enough to include a nice story with his submission. So, here it is, straight from Steve ... [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Seriously, I'm A Genius

2005-08-26 13:27 • by christoofar
for (; aRange <= aValue; aValue -= aRange);

wtf?!?!?!

Re: Seriously, I'm A Genius

2005-08-26 13:30 • by Ken Nipper
42042 in reply to 42041
Ok, I'll say it......"Brillant!"[;)]

Re: Seriously, I'm A Genius

2005-08-26 13:38 • by III
i'm getting old.

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

brillant indeed...

Re: Seriously, I'm A Genius

2005-08-26 13:42 • by Mike R
Wow. This has all the brillance of a true genus.

Re: Seriously, I'm A Genius

2005-08-26 13:44 • by Mike R
42050 in reply to 42048
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



Re: Seriously, I'm A Genius

2005-08-26 13:45 • by DZ-Jay
I gotta ask...



Was her name Paula Bean?



    -dZ.



Re: Seriously, I'm A Genius

2005-08-26 13:45 • by JimNtexas
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.





Re: Seriously, I'm A Genius

2005-08-26 13:46 • by Mike R
42054 in reply to 42050
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))

Re: Seriously, I'm A Genius

2005-08-26 13:46 • by chb
The naming of the variables kills me.

(a|an|the|its)



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

Re: Seriously, I'm A Genius

2005-08-26 13:48 • by kurt
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;

Re: Seriously, I'm A Genius

2005-08-26 13:49 • by Daveh
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).







Re: Seriously, I'm A Genius

2005-08-26 13:53 • by th0mas
i don't see the infinite loop.  Anyone want to fill me in?

Re: Seriously, I'm A Genius

2005-08-26 13:58 • by III
42064 in reply to 42061
>infinite loop

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

Re: Seriously, I'm A Genius

2005-08-26 13:58 • by Henryk Pl&#246;tz
42065 in reply to 42053
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

Re: Seriously, I'm A Genius

2005-08-26 13:59 • by ColdPie
42066 in reply to 42061
RE: Infinite loop



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

Re: Seriously, I'm A Genius

2005-08-26 14:02 • by th0mas
42068 in reply to 42066
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 :).

Re: Seriously, I'm A Genius

2005-08-26 14:02 • by munificent
42069 in reply to 42061
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.

Re: Seriously, I'm A Genius

2005-08-26 14:03 • by ColdPie
42073 in reply to 42068
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?

Re: Seriously, I'm A Genius

2005-08-26 14:04 • by III
42074 in reply to 42066
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?

Re: Seriously, I'm A Genius

2005-08-26 14:04 • by emptyset
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!

Re: Seriously, I'm A Genius

2005-08-26 14:05 • by th0mas
42076 in reply to 42064
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.

Re: Seriously, I'm A Genius

2005-08-26 14:05 • by Daveh
42077 in reply to 42066
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).

Re: Seriously, I'm A Genius

2005-08-26 14:07 • by mpComplete
42079 in reply to 42066
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.


Re: Seriously, I'm A Genius

2005-08-26 14:21 • by Daveh
42085 in reply to 42079
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)!

Re: Seriously, I'm A Genius

2005-08-26 14:24 • by AndrewVos
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

Re: Seriously, I'm A Genius

2005-08-26 14:25 • by Ford Prefect
Warn me to put on my shades before you show me code like this!!

Re: Seriously, I'm A Genius

2005-08-26 14:28 • by chb
int aFractionPortion = 0;
double aValue = (double)anIntegerPortion + 1.0/(double)aFractionPortion;
double aRange = theMax - theMin;
for (; aRange <= aValue; aValue -= aRange);


Tada! <:o)

Re: Seriously, I'm A Genius

2005-08-26 14:31 • by John Smallberries
42093 in reply to 42087
At least no controvery like yeseterday.

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

Re: Seriously, I'm A Genius

2005-08-26 14:43 • by Daniel
42098 in reply to 42069
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.

Re: Seriously, I'm A Genius

2005-08-26 14:59 • by CornedBee
42101 in reply to 42098
Indeed, depending on the FPU setup this might go into an infinite loop
instead of crashing if the second rand() call returns zero.

Re: Seriously, I'm A Genius

2005-08-26 15:02 • by Quietust
42103 in reply to 42101
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.

Re: Seriously, I'm A Genius

2005-08-26 15:22 • by aaron
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)

Re: Seriously, I'm A Genius

2005-08-26 15:25 • by Sean
At least it's pretty well-commented.

Re: Seriously, I'm A Genius

2005-08-26 15:27 • by brazzy
42112 in reply to 42069
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.

Re: Seriously, I'm A Genius

2005-08-26 15:32 • by md2perpe
42113 in reply to 42093
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?"

Re: Seriously, I'm A Genius

2005-08-26 15:37 • by Mike R
42114 in reply to 42113
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.



Re: Seriously, I'm A Genius

2005-08-26 15:38 • by DZ-Jay
42115 in reply to 42068
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.

Re: Seriously, I'm A Genius

2005-08-26 15:41 • by DZ-Jay
42118 in reply to 42110
Sean:
At least it's pretty well-commented.




Yes, the comments are pretty.



    -dZ.

Re: Seriously, I'm A Genius

2005-08-26 15:54 • by christoofar
42125 in reply to 42114
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.

Re: Seriously, I'm A Genius

2005-08-26 16:07 • by The Frog
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

Re: Seriously, I'm A Genius

2005-08-26 16:07 • by EsotericMoniker
42129 in reply to 42055
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.

Re: Seriously, I'm A Genius

2005-08-26 16:09 • by 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!

Re: Seriously, I'm A Genius

2005-08-26 16:14 • by DisturbedSaint
42132 in reply to 42109
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

Re: Seriously, I'm A Genius

2005-08-26 16:23 • by res2
itsSeed also appears to be a global... shame shame!

Re: infinite loop condition

2005-08-26 16:29 • by aikimark

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


Example: theMin := -15  theMax:=0


Also, it might just SEEM like an endless loop if theMin 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)

Re: Seriously, I'm A Genius

2005-08-26 16:33 • by Bustaz Kool
Did the word "MENSA" pepper her speech?

Re: Seriously, I'm A Genius

2005-08-26 16:45 • by Robert
42140 in reply to 42041

 > year or so after she left


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

Re: Seriously, I'm A Genius

2005-08-26 16:51 • by Mung Kee
42142 in reply to 42055
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.

Re: Seriously, I'm A Genius

2005-08-26 16:55 • by database guy
42143 in reply to 42054
Thank you, I don't know why people are so terrified of a using little arithmetic now and then.

Re: Seriously, I'm A Genius

2005-08-26 16:57 • by database guy
42144 in reply to 42054

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.

« PrevPage 1 | Page 2 | Page 3Next »

Add Comment