import java.util.Random; public class RandomBytesExample { public static void main(String[] args) { Random random = new Random(); byte[] bytes = new byte[10]; random.nextBytes(bytes); System.out.println(Arrays.toString(bytes)); } }
import java.util.Random; public class RandomBinaryExample { public static void main(String[] args) { Random random = new Random(); byte[] bytes = new byte[8]; random.nextBytes(bytes); StringBuilder binary = new StringBuilder(); for (byte b : bytes) { binary.append(Integer.toBinaryString(b & 255 | 256).substring(1)); } System.out.println(binary.toString()); } }This code generates an array of 8 random bytes and converts them to a binary string. The java.util.Random class is part of the Java Standard Library, which is included in the java.util package.