I need to create a Java method to return true
or false
randomly. How can I do this?
相关问题
- 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
The class
java.util.Random
already has this functionality:However, it's not efficient to always create a new
Random
instance each time you need a random boolean. Instead, create a attribute of typeRandom
in your class that needs the random boolean, then use that instance for each new random booleans:(Math.random() < 0.5)
returns true or false randomlyYou will get it by this:
This should do:
You can do as following code,
Hope this would help you, Thanks.