Java Random - nextBoolean() Method
The java.util.Random.nextBoolean() method returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
Syntax
public boolean nextBoolean()
Parameters
No parameter is required.
Return Value
Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
Exception
NA
Example:
In the example below, the java.util.Random.nextBoolean() method is used to get pseudorandom, uniformly distributed boolean value.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a random object Random rand = new Random(); //printing uniformly distributed boolean random numbers for(int i = 0; i < 5; i++) System.out.println("Next Boolean Value: " + rand.nextBoolean()); } }
One of the possible outcome is given below:
Next Boolean Value: true Next Boolean Value: false Next Boolean Value: true Next Boolean Value: true Next Boolean Value: false
❮ Java.util - Random