Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to fill byte array with random byte values using nextBytes() in Java

2 Answers

0 votes
package javaapplication1;

import java.util.Random;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            Random random = new Random();
            byte[] arr = new byte[10];

            random.nextBytes(arr);

            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

119 22 96 96 -15 -73 -11 -114 -40 117
    
 */

 



answered Nov 30, 2016 by avibootz
0 votes
package javaapplication1;

import java.util.Random;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            Random random = new Random();
            byte[] arr = new byte[10];

            for (int t = 0; t < 10; t++) {
                random.nextBytes(arr);

                for (int i = 0; i < arr.length; i++) {
                    System.out.print(arr[i] + " ");
                }
                System.out.println();
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

104 94 -112 21 127 -115 58 -87 -93 84 
27 45 -21 22 -19 10 -115 29 -64 60 
106 -110 -31 -25 -97 -26 63 106 -14 75 
-121 118 48 -97 -64 -50 -60 -16 114 -36 
-69 98 97 -91 50 125 -77 100 -127 -2 
68 -126 105 -102 -106 -39 -9 -43 36 21 
108 93 71 77 -105 -65 89 -42 28 36 
-72 -39 -10 -50 6 -46 -48 87 -40 79 
-74 127 -124 -94 114 109 -120 -5 -66 43 
-77 -113 -125 -68 99 -40 9 100 119 -7 
    
 */

 



answered Nov 30, 2016 by avibootz

Related questions

1 answer 175 views
1 answer 87 views
1 answer 157 views
3 answers 261 views
4 answers 281 views
1 answer 67 views
...