Get random boolean in Java

2019-03-11 13:33发布

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 instance returns true or false just once per run. And surprisingly, no matter what I do, every time i get just false

Is there something to improve the method so I can have at least roughly 50% chance to get true?


To make it more understandable: I have my application builded to JAR file which is then run via batch command

 java -jar my-program.jar
 pause

Content of the program - to make it as simple as possible:

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());  
    }
}

If I open 10 command lines and run it, I get false as result every time...

9条回答
叛逆
2楼-- · 2019-03-11 13:57

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)

查看更多
欢心
3楼-- · 2019-03-11 14:00

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.

查看更多
Root(大扎)
4楼-- · 2019-03-11 14:03

you could get your clock() value and check if it is odd or even. I dont know if it is %50 of true

And you can custom-create your random function:

static double  s=System.nanoTime();//in the instantiating of main applet
public static double randoom()
{

s=(double)(((555555555* s+ 444444)%100000)/(double)100000);


    return s;
}

numbers 55555.. and 444.. are the big numbers to get a wide range function please ignore that skype icon :D

查看更多
你好瞎i
5楼-- · 2019-03-11 14:04

Have you tried to look at sun's (oracle) documentation?

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html#nextBoolean()

anyway here's example code:

    java.util.Random

    Random random = new Random();
    random.nextBoolean();
查看更多
Fickle 薄情
6楼-- · 2019-03-11 14:04

You can also make two random integers and verify if they are the same, this gives you more control over the probabilities.

Random rand = new Random();

Declare a range to manage random probability. In this example, there is a 50% chance of being true.

int range = 2;

Generate 2 random integers.

int a = rand.nextInt(range);
int b = rand.nextInt(range);

Then simply compare return the value.

return a == b; 

I also have a class you can use. RandomRange.java

查看更多
爷、活的狠高调
7楼-- · 2019-03-11 14:06
  • Generate an integer random number, INTEGER_RANDOM_NUMBER

  • return (INTEGER_RANDOM_NUMBER & 1)

查看更多
登录 后发表回答