
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Random Bytes in Java
In order to generate random bytes in Java, we use the nextBytes() method. The java.util.Random.nextBytes() method generates random bytes and provides it to the user defined byte array.
Declaration − The java.util.Random.nextBytes() method is declared as follows −
public void nextBytes(byte[] bytes)
where bytes is the byte array.
Let us see a program to generate random bytes in Java −
Example
import java.util.Random; public class Example { public static void main(String[] args) { Random rd = new Random(); byte[] arr = new byte[7]; rd.nextBytes(arr); System.out.println(arr); } }
Output
[B@15db9742
Note − The output might vary on Online Compilers.
Advertisements