Get random boolean in Java

后端 未结 10 1552
情深已故
情深已故 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:05

    Have you tried looking at the Java Documentation?

    Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence ... the values true and false are produced with (approximately) equal probability.

    For example:

    import java.util.Random;
    
    Random random = new Random();
    random.nextBoolean();
    
    0 讨论(0)
  • 2020-12-01 12:05

    The easiest way to initialize a random number generator is to use the parameterless constructor, for example

    Random generator = new Random();
    

    However, in using this constructor you should recognize that algorithmic random number generators are not truly random, they are really algorithms that generate a fixed but random-looking sequence of numbers.

    You can make it appear more 'random' by giving the Random constructor the 'seed' parameter, which you can dynamically built by for example using system time in milliseconds (which will always be different)

    0 讨论(0)
  • 2020-12-01 12:06

    You can use the following for an unbiased result:

    Random random = new Random();
    //For 50% chance of true
    boolean chance50oftrue = (random.nextInt(2) == 0) ? true : false;
    

    Note: random.nextInt(2) means that the number 2 is the bound. the counting starts at 0. So we have 2 possible numbers (0 and 1) and hence the probability is 50%!

    If you want to give more probability to your result to be true (or false) you can adjust the above as following!

    Random random = new Random();
    
    //For 50% chance of true
    boolean chance50oftrue = (random.nextInt(2) == 0) ? true : false;
    
    //For 25% chance of true
    boolean chance25oftrue = (random.nextInt(4) == 0) ? true : false;
    
    //For 40% chance of true
    boolean chance40oftrue = (random.nextInt(5) < 2) ? true : false;
    
    0 讨论(0)
  • 2020-12-01 12:08

    You could also try nextBoolean()-Method

    Here is an example: http://www.tutorialspoint.com/java/util/random_nextboolean.htm

    0 讨论(0)
  • 2020-12-01 12:09

    I recommend using Random.nextBoolean()

    That being said, Math.random() < 0.5 as you have used works too. Here's the behavior on my machine:

    $ cat myProgram.java 
    public class myProgram{
    
       public static boolean getRandomBoolean() {
           return Math.random() < 0.5;
           //I tried another approaches here, still the same result
       }
    
       public static void main(String[] args) {
           System.out.println(getRandomBoolean());  
       }
    }
    
    $ javac myProgram.java
    $ java myProgram ; java myProgram; java myProgram; java myProgram
    true
    false
    false
    true
    

    Needless to say, there are no guarantees for getting different values each time. In your case however, I suspect that

    A) you're not working with the code you think you are, (like editing the wrong file)

    B) you havn't compiled your different attempts when testing, or

    C) you're working with some non-standard broken implementation.

    0 讨论(0)
  • 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);
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题