Languages
[Edit]
EN

Java - generate array of random bytes

2 points
Created by:
JustMike
3488

1. ThreadLocalRandom.nextBytes

byte[] arr = new byte[10];
ThreadLocalRandom.current().nextBytes(arr);

// [-62, -75, 50, -127, 86, -21, 84, 53, 34, -45]
System.out.println(Arrays.toString(arr));


2. Random.nextBytes

byte[] arr = new byte[10];
new Random().nextBytes(arr);

// [-50, -67, 109, -126, 24, 118, -121, -77, 15, -97]
System.out.println(Arrays.toString(arr));


References

ThreadLocalRandom - Java docs
Random - Java docs

Java - random numbers

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.