- 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
Pointlessly overengineered (with the unused symbol arrays,) unable to do the one thing it's supposed to do correctly, and lacking any visible comments. A proper WTF entry.
Admin
The sub can be further simplified to:
sub randomPIN { return sprintf("%06d", rand(1000000)); }
Admin
Well yes, it's clearly over-engineered, but I wouldn't call that an abomination either. At least the path will be obvious when the customer will require that the first digit is not a zero (because, yes, it will happen sooner or later that someone will store the PIN in an Excel sheet, dropping the leading zeroes)
Admin
@Billy, The Dark Lord: this code already assures that. In fact, it will never generate any zeros. @num is filled with 1..9, not 0..9.
Admin
Well, i'll be d.mn.d indeed, I should read the code thoroughly instead of trying to find excuses involving futureproofness through overachievement. My point was that with a little more documentation (and I mean not documenting what the code does, but why it does what it does) it could be acceptable.
Admin
Exactly - after all, ensuring that the first digit is not zero using the simple method would require generating a random number between 100000 and 999999 which I'm not sure is possible in <<insert name of programming language>>
Admin
Maybe the developer was paid per line of code, redundant lines included :)
Admin
An even more subtle bug is that this will RARELY return a 9.
Admin
Bruce Reed, Can you please explain why it would return a 9 any less often than any other number 1-9?
Admin
The real WTF is Perl, Ruby, Python and all these stupid languages.
Admin
random (900000) + 100000 should do the trick... but imho this is less straightforward than generating each digit independantly, each with its own constraints.
Admin
Uhhh...no zeros? Maybe te leap should be:
rand(9)+1
Admin
If indeed the rand function is poorly written (ie does a simple modulo without compensating for the bias in high values) you have a slightly lower probability of getting a 9 than other digits, but (a) with a modulo 10 on a 32 bit random value this should be negligible, and (b) that's a bug in PHP, not in that poor guy's function.
Admin
Solution for Excel sheet:
Since Excel will treat any six-element digit-string as a number, apply a cell format "000000". This formats in six columns left-filled with zeroes.
Admin
c#
(new Random()).Next(100000, 999999 )
Admin
Well, Excel may do some strange things if some creative person has already formatted the target cells as "Text" or some "Custom" abomination. The real problem arises when you try to export your formatted 6-digit string.
Admin
Postscript
6 { rand 9 1 add } repeat
Admin
Using Java and the Xeger library (so we can use a regex!)
... return new Xeger("[0-9]{6}").generate(); ...
Admin
Meh, I've seen worse, like a loop to generate some code that gets evalled.
Admin
I have to hang my head in shame, but not for the reason you think. For the longest time, I had misread the Perl documentation for 'rand'. I had read it as the result would be 0 <= rand(#) <= #, but it's 0 <= rand(#) < #.
In short, calling int(rand(9)) will NEVER return a 9. This is not because of a bad design or implementation. Notice the use of 'int' to get an integer value. rand returns a fractional value >= 0 and less then the number provided. If the number is omitted, "1" is used.
So the "right" answer is as BOFH indicated: sub randomPIN { return sprintf("%06d", rand(1000000)); } And to ensure non-leading zero: sub randomPIN { return sprintf("%06d", rand(900000)+100000); }
Interesting thing I never noticed. format "%##d" will truncate, but "%##.0f" will round (to zero places). I guess I've never used %d format to truncate a number.
sub randomPIN {
Admin
Returning higher numbers less frequently is an advantage. The higher the digit, the longer it takes the rotary dial to get turned and returned on your old rotary phone.
Addendum 2018-05-02 13:16: https://www.cnet.com/news/a-mobile-phone-with-a-rotary-dial-retro-cool-or-retro-fool/
Admin
Sure, this is overly verbose as is, but it's also much easier to tweak if requirements change. "First digit can't be 0" is one example. What about "first character must be a number, second character must be a letter, third character..."? It'd be trivial to change this code to fit the new requirements and the code would still be easily readable.
I imagine that's where this code came from - "adapted" from a different generator that could output more complex stuff.
Admin
"zeros are allowed" are you sure?
Admin
Except that the slowest number on the dial phone is 0.
Admin
Dial telephones use the digits 1 through 10 (with the "0" symbol standing in for ten). Each symbol dialled generates a number of pulses, that the exchange counts; "1" pulses once, "2" pulses twice, etc. Of course, you can't distinguish between the zero pulses from a dialed symbol and the zero pulses from not dialing at all.
Admin
The specification for DTMF tones includes the letters A-D in addition to the numbers 0-9 and the symbols # and *. Most consumer equipment doesn't use the letters, but if the company's desk phones do, maybe their use would be mandated the same way a password policy often requires certain character classes to be represented.
Admin
Sorry, the real WTF is all computer languages. Real programmers don't need computer languages.
Admin
substr(int(rand(999999) + 1000000), 1);
But because repeats are allowed. join("",int(rand(9)) x 6);
Admin
There are non WTF languages like C++, C# and Java, and WTF languages, which is everything else. (Not talking about markup/ declaratory languages like HTML obviously)
Admin
You'd better NOT be talking about me.
Admin
Except if you'd looked at the code, or read the previous comments, you would have known that 0 was already excluded from the results and only digits 1 to 9 were returned.
Admin
My perlish mind immediately wants to one-liner this: return join "", map { int(rand(10)) } (1..5);
Admin
TRWTF is the number of commenters here providing "better" implementations which don't behave the same as the original code, possibly led that way by the similarly-flawed example in the article prior to showing the code.
Admin
Using @num to get the length of the array if not a WTF. The rest of the code is, but using $array[int rand @array] for a random array element is fine.
Admin
Why'd You ever want to store a value as number if You will never do any calculations with it?
Admin
There is even more space dust...
sub randomPIN { sprintf("%06d", rand(1000000)); }
Admin
How is Spring infamous...?
Admin
Except this is not for rotary (or more generally pulse) phones, and this is not a phone number we're talking about.
Admin
You're assuming the original implementation does what it's supposed to. It does something, not necessarily the right thing.