Java Algorithms How to - Get random boolean








Question

We would like to know how to get random boolean.

Answer

import java.util.Random;
/*from  www  . ja  v  a  2  s .  co m*/
public class Main {
  public static final void main(String... args) {
    Random randomGenerator = new Random();
    for (int idx = 1; idx <= 10; ++idx) {
      boolean randomBool = randomGenerator.nextBoolean();
      System.out.println("Generated : " + randomBool);
    }
  }
}