The meaning of the "random string" is not clear in Java.
You can generate random bits and bytes, but converting these bytes to a string is usually not easy, as there is no built-in conversion that takes all arrays of bytes and prints all strings of a given length.
If you need only random bytes, do what theomega suggested and omit the last line.
If you need a random string of some character set, it depends on the set. Base64 is an example of such a set, using 64 different ASCII characters to represent 6 bits each (so 4 of these characters represent 24 bits, which will be 3 bytes).
Paŭlo Ebermann
source share