Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a random boolean with given probability [duplicate]

I am writing java code to solve a problem with simulated annealing method. I need a method to generate a random true only with probability exp(a/b) where a and b are given parameters.

Thanks.

like image 504
Nina Avatar asked Mar 16 '15 09:03

Nina


People also ask

How do you generate a boolean randomly?

In order to generate Random boolean in Java, we use the nextBoolean() method of the java. util. Random class. This returns the next random boolean value from the random generator sequence.

How do you generate a random boolean in C++?

The best way to generate random bool is to use the MSB bit. This is actually a standard Bernoulli distribution with probability 1/2 . Save this answer.

How do you randomize true or false in C#?

If you want to create a random boolean, use this code: var random = new Random(); var randomBool = random.


1 Answers

Assuming that a/b is the percentage probability of returning true:

public boolean exp(double probabilityTrue)
{
    return Math.random() >= 1.0 - probabilityTrue;
}
like image 168
Promination Avatar answered Sep 29 '22 15:09

Promination