Get Random bytes in Java

Description

The following code shows how to get Random bytes.

Example


//from w  w  w. j  a  v  a2 s  .  c o m
import java.util.Arrays;
import java.util.Random;

public class Main {
  public static void main(String[] argv) throws Exception {

    Random rand = new Random();

    byte[] bytes = new byte[5];
    rand.nextBytes(bytes);
    
    System.out.println(Arrays.toString(bytes));
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization