6

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.

Nina
  • 63
  • 1
  • 4
  • Do you mean that the distribution must be in accordance to `f = exp(a/b)`, with f being the density funcion, or that `f = exp(a/b)` is the actual distribution? – fps Mar 16 '15 at 13:31
  • See [my source code](https://github.com/droolsjbpm/optaplanner/blob/master/optaplanner-core/src/main/java/org/optaplanner/core/impl/localsearch/decider/acceptor/simulatedannealing/SimulatedAnnealingAcceptor.java#L88): `double acceptChance = Math.exp(-moveScoreDifference / temperature); if (random.nextDouble() < acceptChance) {...}` – Geoffrey De Smet Mar 17 '15 at 07:02

1 Answers1

11

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

public boolean exp(double probabilityTrue)
{
    return Math.random() >= 1.0 - probabilityTrue;
}
Eric
  • 16,397
  • 8
  • 68
  • 76
Promination
  • 156
  • 6
  • 3
    if you are only using `probability_true` then you can remove the `probability_false` method parameter and also the return statement can be shortened to `return (Math.Random() * 100) > probability_true;` since the evaluation of that statement results in a true or false being returned anyway. – kstandell Mar 16 '15 at 10:18
  • @kstandell I edited the question. The correct form is exp(a/b). The probability of true outcomes is exp(a/b). However, I can customize it for my case. Thanks – Nina Mar 16 '15 at 10:39