How to create an array of 20 random bytes?

novicePrgrmr picture novicePrgrmr · Apr 16, 2011 · Viewed 98.8k times · Source

How can I create an array of 20 random bytes in Java?

Answer

maerics picture maerics · Apr 16, 2011

Try the Random.nextBytes method:

byte[] b = new byte[20];
new Random().nextBytes(b);