Java – Create Random Boolean Value

To create a random boolean value in Java, use Random.nextBoolean() method. Create java.util.Random class object and call nextBoolean() method on this object. nextBoolean() returns a randomly generated boolean value.

In the following example, we will create a random boolean value using nextBoolean() method.

Java Program

</>
Copy
import java.util.Random;

public class Example{ 
    public static void main(String[] args) {
    	Random random = new Random();
    	boolean b = random.nextBoolean();
    	System.out.println("Next random boolean value is : " + b);
    }
}

Output

Next random boolean value is : true

Reference to Random.nextBoolean() method for syntax and more example programs.

Conclusion

In this Java Tutorial, we learned how to create a random boolean value using java.util.Random.nextBoolean() method.