 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I generate random booleans in Java?
To generate random booleans like TRUE or FALSE, at first create a new Random object −
Random randNum = new Random();
Now, loop through the count of booleans you want and generate random booleans with nextBooleans() method −
for (int i = 1; i <= 5; ++i) {    booleanrandomRes = randNum.nextBoolean();    System.out.println(randomRes); } Example
import java.util.Random; public class Demo {    public static final void main(String... args) {       Random randNum = new Random();       System.out.println("Displaying random Booleans...");       for (int i = 1; i <= 5; ++i) {          boolean randomRes = randNum.nextBoolean();          System.out.println(randomRes);       }    } }  Output
Displaying random Booleans... true false false true true
Let us run it again to get distinct random boolean −
Displaying random Booleans... false false true true false
Advertisements
 