- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
Ok, I'll say it......"Brillant!"[;)]
Admin
i'm getting old.
it took me a couple of minutes to figure out how you could get an infinite loop.
brillant indeed...
Admin
Wow. This has all the brillance of a true genus.
Admin
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
Admin
I gotta ask...
Was her name Paula Bean?
-dZ.
Admin
"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.
Admin
[:$] Heh.. replace rand with (float(rand())/float(RAND_MAX))
Admin
The naming of the variables kills me.
(a|an|the|its)
Maybe she's a "hungarian notation fundamentalist" ducks
Admin
Admin
I must say, that while I have made some RNG functions in my time, I never thought of doing it this way...
Admin
i don't see the infinite loop. Anyone want to fill me in?
Admin
doubles are not real numbers - they are approximations. the math that happens with them is therefore imperfect as well.
Admin
Moin,
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 ...
Admin
RE: Infinite loop
What happens in the for loop if theMin==theMax? :)
Admin
Then the function returns 0.0. Read the first five lines of the WTFunction :).
Admin
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.
Admin
Right. My mistake :)
I guess it's just some fudgy stuff with doubles that other people have been mentioning, then?
Admin
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?
Admin
Admin
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.
Admin
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).
Admin
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.
Admin
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)!
Admin
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
Admin
Warn me to put on my shades before you show me code like this!!
Admin
Admin
At least no controvery like yeseterday.
I think we all can agree this is WTFery of the highest caliber.
Admin
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.
Admin
Indeed, depending on the FPU setup this might go into an infinite loop instead of crashing if the second rand() call returns zero.
Admin
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.
Admin
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)
Admin
At least it's pretty well-commented.
Admin
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.
Admin
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?"
Admin
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.
Admin
WTFunction ... OMG!!! That is the funniest thing! Ha, ha, ha!
-dZ.
Admin
Yes, the comments are pretty.
-dZ.
Admin
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.
Admin
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
Admin
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.
Admin
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!
Admin
Wouldn't the constraint on the for loop remove this case?
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
Admin
itsSeed also appears to be a global... shame shame!
Admin
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)
Admin
Did the word "MENSA" pepper her speech?
Admin
> year or so after she left
Women and Computer Science don't mix! ;-)
Admin
I was going to jokingly make this comment, but figured I'd better not after yesterdays post.
Admin
Thank you, I don't know why people are so terrified of a using little arithmetic now and then.
Admin
Thank you, I don't know why people are so terrified of a using little arithmetic now and then.