Java Random.nextBytes(byte[] bytes)

Syntax

Random.nextBytes(byte[] bytes) has the following syntax.

public void nextBytes(byte[] bytes)

Example

In the following code shows how to use Random.nextBytes(byte[] bytes) method.


/* w w  w  .  jav  a2  s .c  o m*/

import java.util.Arrays;
import java.util.Random;

public class Main {
   public static void main( String args[] ){

      Random randomno = new Random();
      
      // create byte array
      byte[] nbyte = new byte[30];
      
      // put the next byte in the array
      randomno.nextBytes(nbyte);
   
      // check the value of array   
      System.out.println("Value of byte array: " +Arrays.toString(nbyte));
   }      
}

The code above generates the following result.