- 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
They are. Not in home machines because you don't need that much security there (actually you need it but nobody seems to bother) but I've seen servers being shipped with these thermal noise sampling gadgets.
Admin
But that's a key difference. If I have a 32-bit internal seed, and I return the lower 16-bits as the new value, then having 23456 follow 12345 once, does not mean that it will follow 12345 next time.
No. It limits you to 65536 different sequences of numbers, and since we are calling it 5 times to build one number, it's the sequence that is important.
To demostrate, let's say I has a (very bad) PRNG which just returns this sequence: 1,2,3,4,5,1,2,3,4,5 over & over. If I were to build IDs using the formula (rnd * 10 + rnd), then I'd get only 5 ids : 12, 34, 51, 23 & 45. This is your point. However, my point is that if a different seed were to give the same numbers in a different sequence, (say, 2,4,1,3,5), we'd get another 5 ids: 24, 13, 52, 41, 35. If 5 different seed give us 5 different sequences, then we have 25 different ids possibly generated.
Admin
What version of Java has Random.nextBytes(byte[]) (static method)? Shouldn't it be:
Admin
One might argue (and might even be right) that you should never write a block that's longer than one page but if you do, just put those comments there. It won't make your code any worse but maintainers will surely love you. Being one of those poor souls who try to maintain the unmaintainable, all I can say is that while I do have an IDE , I appreciate every little attempt of the coders to help my job. Even if it doesn't help too much.
Admin
But he's not talking about fractional byte, but fractional bits, (which, being the atoms of computer logic, cannot be subdivided)
Admin
Here is the Random class encapsulated in an InputStream:
Admin
"American English", both the phrase and what it refers to, is a WTF itself. The commas in that case and this one go outside the quotes. Commas go within quotes to denote more speech to follow, which is not the case here.
Admin
Hmmm. Back in the days of ASM51, there was a pagebreak directive for just this purpose ("if you really want to print the listing, let's help make it readable"). Hell, there probably still is. But not in C... It's a page layout issue, which is quite different from code quality issues. Separate the issues and survive.
In general, I'd agree. But I've seen comments go stale too often. Including such "here endeth the loop" comments. Comments tend to become wrong/misleading over time. Nowadays, I prefer (to read and write) code that's so transparent that we don't need no steenking comments.
Steve
captcha: captcha. blimey: blimey.
Admin
As the guy who submitted this WTF, I'll make a quick defense of my "fix". First, what I posted here was dashed off on the fly in the submission form, without a compiler handy and without bothering to look up whether nextBytes() is static. The real version uses SecureRandom and works properly. Furthermore, the bytes returned from this method aren't used directly, but are used to seed a different PRNG algorithm.
As the security geeks have pointed out, even SecureRandom isn't all that entropic, but it's the best thing at my disposal, and it's a hell of a lot better than what came before. If there's a better option that makes the paranoid happy and will run (1) quickly, (2) without requiring user interaction (e.g. by moving the mouse to generate entropy), and (3) without touching the internet (e.g. no calls to HotBits), then let me know.
Admin
Yep, quite right. The comma in this case belongs outside the quote. Putting it inside the quote results in a syntax error and a fatal exception in my brain. Have mercy, please.
Meanwhile, back at the original post: "getRandomBits() returns a 32-byte array of random bites" and "in fact a proper implementation can be writting in five lines"... Bites? Writting? WTF?
Steve
Admin
Yes, CryptGenRandom and similar parts of the Win32 CryptoAPI have these functions. They are based on a Yarrow-like secure RNG.
Admin
Admin
The basic idea of being secure is do not trust to anybody. The guy who wrote the snippet obviously didn't trust to securerandom so he wrote it his way and he proved good skills by doing so. I would never trust to any set of "random numbers" coming out of a blackbox which is 1's and 0's and tick - tack - tick - tack and honestly don't know how to implement features like mouse capturing if the code is supposed to run on a handset. I hope it's hard to predict when the System.gc() comes ;-)
Admin
Admin
re:
i like these in particular
} // end for
} // end if hash_bytes
} // end if val non null
} // end if key non null
} // end while e.hasMoreElements
so THAT's what }'s do...!
-------------------------
so what's wrong with that? when a code gets long enough to actually scroll the screen, this is one way to keep track of which ending caret goes with which beginning caret. I used to do this when I was coding using a vanilla text editor.
Admin
Xandax:
Actually
For a looooong block of code, sure. But for code you can see all on one page? Like for a 3 line for loop? You must be kidding.
-----------------------
you must me shortsighted not to anticipate a block of code getting longer in the future. there are some times when a block of code becomes really long before refactoring.
Admin
Actually, this would be even better because calling getRandomBytes twice in one millisecond will result in different values:
import<font size="2"> java.util.Random;
public<font size="2"> </font><font color="#7f0055" size="2">class</font><font size="2"> ByteUtils {
<font color="#7f0055" size="2">private</font><font size="2"> </font><font color="#7f0055" size="2">static</font><font size="2"> </font><font color="#7f0055" size="2">long</font><font size="2"> </font><font color="#0000c0" size="2">seed</font><font size="2"> = System.currentTimeMillis() ;
<font color="#7f0055" size="2">public</font><font size="2"> </font><font color="#7f0055" size="2">static</font><font size="2"> </font><font color="#7f0055" size="2">byte</font><font size="2">[] getRandomBytes ( ) {
<font color="#7f0055" size="2">byte</font><font size="2">[] retVal = </font><font color="#7f0055" size="2">new</font><font size="2"> </font><font color="#7f0055" size="2">byte</font><font size="2">[32] ;
Random random = <font color="#7f0055" size="2">new</font><font size="2"> Random(</font><font color="#0000c0" size="2">seed</font><font size="2">) ;
random.nextBytes(retVal) ;
<font color="#0000c0" size="2">seed</font><font size="2"> = random.nextLong() ;
<font color="#7f0055" size="2">return</font><font size="2"> retVal ;
}
}
</font></font></font></font></font></font></font></font><font size="2"> </font><font color="#7f0055" size="2">class</font><font size="2"> ByteUtils {<font color="#7f0055" size="2">private</font><font size="2"> </font><font color="#7f0055" size="2">static</font><font size="2"> </font><font color="#7f0055" size="2">long</font><font size="2"> </font><font color="#0000c0" size="2">seed</font><font size="2"> = System.currentTimeMillis() ;
<font color="#7f0055" size="2">public</font><font size="2"> </font><font color="#7f0055" size="2">static</font><font size="2"> </font><font color="#7f0055" size="2">byte</font><font size="2">[] getRandomBytes ( ) {
<font color="#7f0055" size="2">byte</font><font size="2">[] retVal = </font><font color="#7f0055" size="2">new</font><font size="2"> </font><font color="#7f0055" size="2">byte</font><font size="2">[32] ;
Random random = <font color="#7f0055" size="2">new</font><font size="2"> Random(</font><font color="#0000c0" size="2">seed</font><font size="2">) ;
random.nextBytes(retVal) ;
<font color="#0000c0" size="2">seed</font><font size="2"> = random.nextLong() ;
<font color="#7f0055" size="2">return</font><font size="2"> retVal ;
}
}
</font></font></font></font></font></font></font><font color="#7f0055" size="2">private</font><font size="2"> </font><font color="#7f0055" size="2">static</font><font size="2"> </font><font color="#7f0055" size="2">long</font><font size="2"> </font><font color="#0000c0" size="2">seed</font><font size="2"> = System.currentTimeMillis() ;<font color="#7f0055" size="2">public</font><font size="2"> </font><font color="#7f0055" size="2">static</font><font size="2"> </font><font color="#7f0055" size="2">byte</font><font size="2">[] getRandomBytes ( ) {
<font color="#7f0055" size="2">byte</font><font size="2">[] retVal = </font><font color="#7f0055" size="2">new</font><font size="2"> </font><font color="#7f0055" size="2">byte</font><font size="2">[32] ;
Random random = <font color="#7f0055" size="2">new</font><font size="2"> Random(</font><font color="#0000c0" size="2">seed</font><font size="2">) ;
random.nextBytes(retVal) ;
<font color="#0000c0" size="2">seed</font><font size="2"> = random.nextLong() ;
<font color="#7f0055" size="2">return</font><font size="2"> retVal ;
}
}
</font></font></font></font></font></font><font color="#7f0055" size="2">public</font><font size="2"> </font><font color="#7f0055" size="2">static</font><font size="2"> </font><font color="#7f0055" size="2">byte</font><font size="2">[] getRandomBytes ( ) {<font color="#7f0055" size="2">byte</font><font size="2">[] retVal = </font><font color="#7f0055" size="2">new</font><font size="2"> </font><font color="#7f0055" size="2">byte</font><font size="2">[32] ;
Random random = <font color="#7f0055" size="2">new</font><font size="2"> Random(</font><font color="#0000c0" size="2">seed</font><font size="2">) ;
random.nextBytes(retVal) ;
<font color="#0000c0" size="2">seed</font><font size="2"> = random.nextLong() ;
<font color="#7f0055" size="2">return</font><font size="2"> retVal ;
}
}
</font></font></font></font></font><font color="#7f0055" size="2">byte</font><font size="2">[] retVal = </font><font color="#7f0055" size="2">new</font><font size="2"> </font><font color="#7f0055" size="2">byte</font><font size="2">[32] ;Random random = <font color="#7f0055" size="2">new</font><font size="2"> Random(</font><font color="#0000c0" size="2">seed</font><font size="2">) ;
random.nextBytes(retVal) ;
<font color="#0000c0" size="2">seed</font><font size="2"> = random.nextLong() ;
<font color="#7f0055" size="2">return</font><font size="2"> retVal ;
}
}
</font></font></font></font><font color="#7f0055" size="2">new</font><font size="2"> Random(</font><font color="#0000c0" size="2">seed</font><font size="2">) ;random.nextBytes(retVal) ;
<font color="#0000c0" size="2">seed</font><font size="2"> = random.nextLong() ;
<font color="#7f0055" size="2">return</font><font size="2"> retVal ;
}
}
</font></font></font><font color="#0000c0" size="2">seed</font><font size="2"> = random.nextLong() ;<font color="#7f0055" size="2">return</font><font size="2"> retVal ;
}
}
</font></font><font color="#7f0055" size="2">return</font><font size="2"> retVal ;}
}
</font>Admin
I only pasted a class with one method. There's something seriously wrong with this forum.
Admin
I think the author was trying to generate true-random numbers using real entropy, which pseudo-random algorithms can't do. This is a non-trivial task, and the snippet is a good attempt, though not the best. There is a whole industry around deriving ways to use true entropy as a source of randomness in software, from casino applications to simulation programs.
I think the real WTF here is the person who submitted the entry. Just because you don't understand it doesn't make it ridiculous.
Admin
On top of that, this sentence is not a comma splice. The commas are all in acceptable positions.
As to "spell-checker", this is an acceptable, if not common, usage:
http://dictionary.reference.com/search?q=%22spell-checker%22
Escalating a flame war is only recommended if you have actual fire handy, and not just smoke.
Admin
Yeah, right, wtf is it doing here ? Isn't it kind of security issue too ? This code should never get out in the first place.
Admin
I do Random Video, Random Start points and the same for music
Randomly play the family album of 5000+ pics.
Good going even if it doesn't work.
A few CPU cycles, who cares
Admin
Excellent!
"At the end of the competition, the winner will be entrophied on the main stage."
Admin
It becomes a reflex after a while and you just type it, especially if you immediately add a closing brace when you start your open.
In VB I automatically type 'i after I type Next because I've done it so much. ( For i = 0 to ... Next 'i )
Admin
Actually, why not refacter each block into an easy to understand function?
Admin
You don't need to. The RNG used by Java has been specified in the language specification since Java 1.0. You can view the algorithm used directly from the java.util.Random javadoc. (The code is included in the individual method descriptions - scroll down.)
Unfortunately, because the implementation is spelled out, it also can't be changed, and as other people have mentioned, the default Java implementation of random isn't, exactly, random.
Admin
Some kids in my computer graphics class were presenting their project, which was a randomly generated landscape scene, and they were talking about how the C standard library's pseudorandom number generator "wasn't random enough" so they implemented their own.
Admin
Thats the F** problem with C/x. Try Progress. The best 4GL ever written if we talk enterprice. And every time I suggest Progress, all you C+ fuckers object.
But all of you object because your focus is on C. Progress makes live easy.
By the way, I think I am in love with the Bean Bag girl.
Admin
That is a very, very good practice. In case of such deep functions, it makes understanding and debugging the code a lot easier. I've spent hours trying to figure out spaghetti code - which would have been way easier if every } would have had a comment denoting the end of which it shows.
Of course, what do I know - even the captcha text for this posting is "stfu"...
Admin
"As long as you seed it with the time (or similar) they should be ok, right? It's not like they cryptographically secure random number sequences."
Admin
hmm .. I hope you're kidding. '}' is not an overloaded operator (in fact not even an operator at all) but simply a symbol that denotes the end af a block, ANY block.
I can write
And btw, if anyone feels the need to comment the end of block maybe they should think about refactoring their method.{
int foo = 5;
System.out.println(foo);
}
at any point within a method; it doesn't need to be preceeded by "if", "for", "while" or anything for that matter. A block is simply a statement which (a) contains 0 or more statements and (b) limits the scope of those statements.
Admin
If you need to next a for inside an if inside an if inside a while then I reckon you've got at least 3 extract function refactorings to do there.
Or to put it another way... if you've got a closing brace that's too far away to see its matching brace clearly without comments, then you're writing far too large a monolithic block of code.
Most of the time.
Admin
You're right but as I mentioned above, if you happen to write code like this then at least put a comment on the closing braces, making it much easier for your successor to refactor it properly.
Admin
If you need comments like that, that's a good sign that your block is getting too long and indented too deeply.
Admin
Guess what new Random() does...
Admin
Not always the case... I've seen people comment end braces when the start of the block was two line above.
I think commenting a brace is rather useful when reading other peoples code.
Admin
No, he proved that he's a total idiot and unskilled programmer by doing so. Blindly trusting a blackbox implementation isn't wise, but rolling your own ad-hoc solution without any theoretical foundation is far, far worse. What you should do is take a standard implementation like SecureRandom and have it inspected closely by someone who actually knows the math behind cryptography. Rolling your own is, as proven here, likely to yield a something much worse than even the weakest standard solution.
Um no, it's in fact an absolutely awful attempt. He is using NO "real entropy" whatsoever. A hint "real entropy" is not something you get by writing long, complicated code.
Admin
I've used this numerous times:
http://www.exampledepot.com/
Admin
If by that you mean comments that do nothing but indicate which block is being closed, then that's what I would call "worse than useless" comments, because it adds no information that isn't immediately visible in properly indented code, and instead just clogs up the view of said code.
Cleanly formated code that is properly divided into units (methods, classes) of appropriate size and uses meaningful identifiers needs very few comments to help understanding. Commends that say things which are already obvious from looking at the code hinder understanding. I'm talking about stuff like:
I can see from the method name that it gives me a bloody xyzzy and how it's implemented! Ironically, this kind of commend usually skips over the stuff that would actually need explanation, namely what the hell a "xyzzy" is.
Admin
It seems to me that instead of doing that properly he managed to create a random number system that is highly predictable if combined with a tiny information leak from elsewhere.. That piece of code could be extremely DANGEROUS imo.
Admin
Wow, this comment is the real wtf. Any kind of real security does not rely on being unknown. While such security may work in the short term, in the long term it won't Your security measures will get out (or you may not have needed them because noone cares). In fact, if you really care about security, let the whole world know what you are doing and invite them to break in. Give out a nice gift certificate for anyon who succeed and tells you how, then fix your security. This concept has been know for more than 150 years. Sure, computers aren't that old...but lockmakers of olden times knew this concept as well. The only hidden information your lock should have is the key. That's where most security breaks anyway. Keys (passwords) are chosen by people. People think their [birthday|pets name|zipcode|etc] is a good password.
As to the } comment discussion. Mostly I'm pro comment because people tend to undercomment rather than over comment. So I say if it floats your boat, do it. However if you are doing it because your code doesn't fit on your screen, then I hope you are programming on your cell phone. Any procedure that scrolls off the screen is too long by far. there is never a good reason for it. never. even though you're not supposed to ever say never. If you happen to find a counterexample which you can mathematically, socially, aesthetically and any otherly prove to be correct, even then you should'nt do it.
Admin
Random points generated with linear congruential prng all fall on parallel planes, so it is quite possible that standard prng was not good enough. Google "spectral test".
Admin
Well, you could always stick a form-feed character in (ASCII 0x0C, IIRC). Assuming the printout respects such things, of course.
Admin
That's interesting, I though the char for 0x0c was a<font size="-1"> carriage return and 0x0A was a line-feed</font>
<font size="-1">CAPTCHA = wtf - makes sense</font>
Admin
the code example you gave may need heavy commenting because, it's not good code (i.e that whole function can be done in one line, return Integer.parseInt(this.xyzzyString); ;)). But I do understand what you mean. there's a fine line between to much commenting and not enough.
Admin
(You know you've been trawling through too many Wintel hex dumps when you have got used to the 0D 0A sequence.)
Admin
Every single comment there is either completely pointless, or actually outright wrong. And the comment that is wrong would have been completely pointless. As brazzy was saying, the only comment required is the one that is actually missing - namely what the hell the point of a xyzzy is.
Commenting is not there to be a lesson in programming. I only want to see a comment if it is (a) something the code itself doesn't say (like what the xyzzy is for), (b) clarification of something tricky, or (c) an executive summary of what something does. Oh, and stuff being fed to JavaDoc or the like, but that counts as (a) and (c) combined.
(Oh dear God, this forum editor is still badly broken and wanting to put no-break spaces in all the time if I don't sit on its head and beat it till it's blue.)
Admin
Say, language purist, shouldn't that have been randomnest random that could be randomned? ;-)
Admin
Heh sure should never rely on being "unknown" to implement security, they say using the name of your secret mistress as your password is good only to find out that everybody around knows about your relationship. I've been talking about possibility of revealing existing security holes to public. Half of the comments here says the code is OK, the other half says it's BAD, I'm not a crypto coding expert so I can't really say whether the SOD-random-generator is or is not a real security hole, but something in it definitely smells.
Admin
No Quack. Ctrl-M / 0x0D / Chr(13) is CR. FF is indeed Ctrl-L / 0x0C / Chr(12).