How can I create an array of 20 random bytes in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
If you are already using Apache Commons Lang, the
RandomUtils
makes this a one-liner:Java 7 introduced ThreadLocalRandom which is isolated to the current thread.
This is an another rendition of maerics's solution.
Try the
Random.nextBytes
method:Create a Random object with a seed and get the array random by doing:
If you want a cryptographically strong random number generator (also thread safe) without using a third party API, you can use
SecureRandom
.Java 6 & 7:
Java 8 (even more secure):