- 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
Well the frist image is bound to be the best.
Admin
and the last image must be the worst
Admin
I must be missing something. How does 2/5 come to 1/3? Also, why no mention of the fact that it never displays the last image?
Admin
So a small code snipped from way back has a couple of bugs that end-users probably never noticed!
Where's the WTF?
Admin
myimages.length returns 5 making the possible values it returns, 0, 1, 2, 3, 4, 5 (6 options). 0 is effectively 1 so the probability of it showing the first image (returning 0 or 1) is 2/6 or 1/3. This also means it would return the last image 1/6 of the time.
Admin
Pretty lame really.
Admin
Dunno but I feel like I'm being trolled...
Zero is removed, not replaced. Permutations thus include 1, 2, 3, 4, 5, which add up to a total of 5, not 6.
But yes, it seems that
Admin
Probably arose when the boss said: "Every pretty damn often it duzzn work! Fix it!"
"Oh yeah," replies Roger the Bodger. It's returning a zero. Damn, haven't got time to work out how to fix it properly, oh sod it just make it 1. That gets him off my back. Who gives a damn anyway? Now back to (enter cool game of choice for the era this was written)."
Admin
... but the real WTF is indenting the "document.write" command to the same level as the "ry = 1" command so as to fool the incautious developer into thinking it is part of the if clause. Oh veeeeeeery cunning.
Admin
Would have been better if ry=1 and document.write were in { }. Zen it would be totally random generator of first image.
Admin
myimages.length actually returns 6, but the rest of your argumentation is right.
Admin
well, it is inside the if clause. In effect, the first image is displayed 1/6 of the time, and nothing is shown in any other case...
Admin
Arrays in Javascript are 0-based, so there is an array element with index = 0, although it has no value. So the array length is 6, and the random function will return a value between 0 and 5: all elements can be retrieved, but the element with index 1 is twice as likely to be chosen.
Admin
Do you see an opening bracket after the if? I don't...
Admin
Admin
Admin
It's not valid Javascript anyways without semicolons ending the statements. But I guess most browsers are quite tolerant w.r.t that.
Admin
So, the author doesn't know (or doesn't care) that Javascript arrays are zero-based, practices bad indentation and seems to have difficulties with generating a random number from the range 1..5, leading to image 1 being shown more often than the others.
If this code indeed hails from the late 90s, then this has to be considered normal or at least as soemthing that does not merit a wtf. In that epoch, Javascript stuff like that usually was hacked together by webmasters not by programmers.
The document.write() however definitely is a wtf: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice
I mean, a random image usually is expected to appear at a specific place inside the webpage, not wherever document.write feels like placing it.
Admin
The ECMAScript standard allows omitting the semicolons in some cases, but they are recommended to avoid some ambiguities.
Admin
Then you run your code through a minifier and find out that the result doesn't work anymore.
Luckily you can find the missing semicolons easily enough with a regular expresion or two.
Admin
No it's not. Go away and study JavaScript 101 and then report back as to why it's not inside the if clause.
Admin
It's a bracketless if statement. Just paste this in your browser's dev console or whatever:
(Misleading indent intentional)
Admin
As is the == comparison and missing semicolons. Just making sure the context is appropriate for when you might see it in the wild.
Admin
Wow, I first read that still in Python mode; which would mean:
if (ry==0) { ry=1 document.write('[image]') } A big WTF.
Seeing the entire picture, maybe they wanted to get the first picture the most exposure, if so the WTFness of the article diminishes greatly.
Admin
Ahh, another WTF scraped from the scratched-up bottom of the barrel. Yay.
Admin
Janeane Garofolo
Captcha: quis. Exactly. Quis est Jeanne Garofolo?
Admin
I'm guessing what happened is that the original coder believed that the random expression would (in the case cited here) return a value from 1 to 5. The mysteriously, there was an error, and someone told him that the expression could return zero at times. So he coded to deal with the zero.
I'd rate this about one and a half stars out of four.
Admin
If the length is indeed 5 then the random code selects a number in the range 0 to 4 and the 5th image is never shown. We end up with 40% image 1, 20% each for images 2,3 & 4 and never show 5. It may not be desperately horrible code but it is reasonably obvious that it really doesn't do what was intended.
Admin
heh reminds me of a riddle/js WTF:
what does the followin equate to?
(function(){return 1})()
answer: undefined
Admin
You're not being trolled. The quoted if statement is as follows: if (ry==0) ry=1 document.write('[image]') }
So, we have a randomizer that will create a number between [0..6). Anytime a 0 occurs, it is changed to a 1. So, the total possible values are [1,1,2,3,4,5].
But the other problem I see is that an image will only be displayed when the randomizer returns a 0 value. The document.write statement is within the if clause. And, as such, it will only ever display image 1.
Of course, if the code read as such: if (ry==0) ry=1 } document.write('[image]')
Then an image is displayed every time and the first image as a 1/3rd chance of being displayed.
Admin
Okay, I'm stupid and got caught by the misleading indentation of the document.write statement. Sorry.
But that doesn't detract from the results of the randomizer and if statement combination returning any one of [1,1,2,3,4,5]
Admin
Honestly, looks like the kind of code an inexperienced developer might write when just learning and needing to fulfill a requirement to have random images. The 1-based array is indicative of a Visual Basic coder.
Certainly a WTF, but one that's understandable, for whatever that's worth.
Admin
Googling the comment in the code, it looks like this was copied and pasted from one of many JavaScript sites (eg. I found it at http://www.javascriptkit.com/script/cut144.shtml). Most likely the developer didn't know what they were actually doing.
Admin
Admin
Admin
Hmm ... it seems this has caught out plenty of punters today ... it's a bit like dropping a penny in front of a marathon. The leader will of course stop dead to pick it up and everybody will pile into him causing an amusing pile of mayhem.
Admin
Admin
Admin
Admin
the real WTF are:
Admin
Admin
Admin
I think qbolec wins some internets.
Admin
That's not a mistake. It's an NSA backdoor meant to look like a mistake by subverting the PRNG.
Admin
Admin
push has been added as part of Javascript 1.3 (http://web.archive.org/web/20000815081640/http://developer.netscape.com/docs/manuals/communicator/jsref/js13.html) and the script might predate that (no, I did not check whether functions used in the script indicate that it can not have originated before Javascript 1.3).
Admin
Would someone like to explain this one for me?
Admin
As someone who actually lived then: The style of code indicates an author who would have been at home with AWK and maybe had some background in Pascal. (Note that it's not just simple plain code, but is enclosed in a named function, this tells of some discipline, while the missing semicolons are expressing some contempt for JS, as expressed by many "serious" IT folks of the time.)
Now (opinions expressed here are/were not mine, but actually existed):
Admin
The real WTFs are in the comments. It's a zero based array; element 0 isn't filled but it still counts, so the array size is 6.
ry when initially set, has an equal probability distribution of among values 0, 1, 2, 3, 4, or 5. P(0)=(1/6) and P(1)=(1/6), so the probability of the first image being displayed is actually 1/3.
Also the last image will in fact be displayed 1/6 of the time.
Admin
if(post=='zreoth') post=='frist'