Get random boolean in Java

后端 未结 10 1574
情深已故
情深已故 2020-12-01 11:28

Okay, I implemented this SO question to my code: Return True or False Randomly

But, I have strange behavior: I need to run ten instances simultaneously, where every

10条回答
  •  甜味超标
    2020-12-01 12:20

    Why not use the Random class, which has a method nextBoolean:

    import java.util.Random;
    
    /** Generate 10 random booleans. */
    public final class MyProgram {
    
      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);
        }
      }
    }
    

提交回复
热议问题