Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Random nextBoolean() method in Java with Examples
Next article icon

Random nextBytes() method in Java with Examples

Last Updated : 07 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

The nextBytes() method of Random class places the generated random bytes into an user-supplied byte array.

Syntax:

  public void nextBytes(byte[] bytes)    

Parameters: The function accepts a single parameter bytes which is the non-null byte array in which to put the random bytes.

Return Value: This method has no return value.

Exception: The function does not throws any exception.

Program below demonstrates the above mentioned function:

Program 1:




// program to demonstrate the
// function java.util.Random.nextBytes()
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // create random object
        Random r = new Random();
  
        // create byte array
        byte[] bytes = new byte[10];
  
        // put the next byte in the array
        r.nextBytes(bytes);
  
        // print random value of array
        System.out.print("Random bytes = [ ");
        for (int i = 0; i < bytes.length; i++) {
            System.out.printf("%d ", bytes[i]);
        }
        System.out.print("]");
    }
}
 
 
Output:
  Random bytes = [ -90 -126 -75 50 -117 -13 -55 -63 -117 47 ]  

Program 2:




// program to demonstrate the
// function java.util.Random.nextBytes()
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // create random object
        Random r = new Random();
  
        // create byte array
        byte[] bytes = new byte[15];
  
        // put the next byte in the array
        r.nextBytes(bytes);
  
        // print random value of array
        System.out.print("Random bytes = [ ");
        for (int i = 0; i < bytes.length; i++) {
            System.out.printf("%d ", bytes[i]);
        }
        System.out.print("]");
    }
}
 
 
Output:
  Random bytes = [ -82 75 -105 41 -34 94 81 10 -107 -46 37 4 -1 100 -119 ]  


Next Article
Random nextBoolean() method in Java with Examples

T

Twinkl Bajaj
Improve
Article Tags :
  • Java
  • Java-Functions
  • Java-Random
Practice Tags :
  • Java

Similar Reads

  • SecureRandom nextBytes() method in Java with Examples
    The nextBytes() method of java.security.SecureRandom class is used to generate a user-specified number of random bytes. If a call to setSeed had not occurred previously, the first call to this method forces this SecureRandom object to seed itself. This self-seeding will not occur if setSeed was prev
    3 min read
  • Random next() method in Java with Examples
    The next() method of Random class returns the next pseudorandom value from the random number generator's sequence. Syntax: protected int next(int bits) Parameters: The function accepts a single parameter bits which are the random bits. Return Value: This method returns the next pseudorandom number.
    1 min read
  • Random nextDouble() method in Java with Examples
    The nextDouble() method of Random class returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence. Syntax: public double nextDouble() Parameters: The function does not accepts any parameter. Return Value: This method returns th
    1 min read
  • Random nextBoolean() method in Java with Examples
    The nextBoolean() method of Random class returns the next pseudorandom, uniformly distributed boolean value from the random number generator's sequence. Syntax: public boolean nextBoolean() Parameters: The function does not accepts any parameter. Return Value: This method returns the next pseudorand
    1 min read
  • Random nextFloat() method in Java with Examples
    The nextFloat() method of Random class returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from the random number generator's sequence. Syntax: public float nextFloat() Parameters: The function does not accepts any parameter. Return Value: This method returns the nex
    1 min read
  • Random nextLong() method in Java with Examples
    The nextGaussian() method of Random class returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. Syntax: public long nextLong() Parameters: The function does not accepts any parameter. Return Value: This method returns the next pseudorandom, uni
    1 min read
  • Scanner nextByte() method in Java with Examples
    The nextByte(radix) method of java.util.Scanner class scans the next token of the input as a Byte. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextByte(radix) where the radix is assumed to be t
    5 min read
  • Random nextGaussian() method in Java with Examples
    The nextGaussian() method of Random class returns the next pseudorandom, Gaussian(normally) distributed double value with mean 0.0 and standard deviation 1.0 from the random number generator's sequence. Syntax: public double nextGaussian() Parameters: The function does not accepts any parameter. Ret
    1 min read
  • Random setSeed() method in Java with Examples
    The setSeed() method of Random class sets the seed of the random number generator using a single long seed. Syntax: public void setSeed() Parameters: The function accepts a single parameter seed which is the initial seed. Return Value: This method has no return value. Exception: The function does no
    2 min read
  • Field setByte() method in Java with Examples
    setByte() method of java.lang.reflect.Field used to set the value of a field as a byte on the specified object. When you need to set the value of a field of an object as byte then you can use this method to set value over an Object. Syntax: public void setByte(Object obj, byte b) throws IllegalArgum
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences