Android How to - Make a random byte array








Question

We would like to know how to make a random byte array.

Answer

/* w  w w.  j av a2  s.  c  om*/
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class Main {
  
  // Make a random byte array (needs to be better)
  public static byte[] makeRandom(int len) {
    byte [] out = new byte[len];    
  
     Random randomGenerator = new Random();
     for (int i = 0; i <= len-1; ++i) {
       int ran = randomGenerator.nextInt(255);
       out[i] =(byte) ran;
     }
     return out;
  }
  
}