Does Java have any functionality to generate random characters or strings? Or must one simply pick a random integer and convert that integer’s ASCII code to a character?
Does Java have any functionality to generate random characters or strings? Or must one
Share
There are many ways to do this, but yes, it involves generating a random
int(using e.g.java.util.Random.nextInt) and then using that to map to achar. If you have a specific alphabet, then something like this is nifty:Do note that
java.util.Randomis actually a pseudo-random number generator based on the rather weak linear congruence formula. You mentioned the need for cryptography; you may want to investigate the use of a much stronger cryptographically secure pseudorandom number generator in that case (e.g.java.security.SecureRandom).