Probability in Java

前端 未结 5 680
借酒劲吻你
借酒劲吻你 2020-12-04 21:57

I was curious to know, how do I implement probability in Java? For example, if the chances of a variable showing is 1/25, then how would I implement that? Or any other proba

相关标签:
5条回答
  • 2020-12-04 22:03

    Java has a class called java.util.Random which can generate random numbers. If you want something to happen with probability 1/25, simply generate a random number between 1 and 25 (or 0 and 24 inclusive) and check whether that number is equal to 1.

    if(new java.util.Random().nextInt(25)==0){
        //Do something.
    }
    
    0 讨论(0)
  • 2020-12-04 22:06

    Since 1.7 it's better to use (in concurrent environment at least):

    ThreadLocalRandom.current().nextInt(25) == 0
    

    Javadoc

    A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools. Usages of this class should typically be of the form: ThreadLocalRandom.current().nextX(...) (where X is Int, Long, etc). When all usages are of this form, it is never possible to accidently share a ThreadLocalRandom across multiple threads.

    This class also provides additional commonly used bounded random generation methods.

    0 讨论(0)
  • 2020-12-04 22:14

    You'd use Random to generate a random number, then test it against a literal to match the probability you're trying to achieve.

    So given:

    boolean val = new Random().nextInt(25)==0;
    

    val will have a 1/25 probability of being true (since nextInt() has an even probability of returning any number starting at 0 and up to, but not including, 25.)

    You would of course have to import java.util.Random; as well.

    As pointed out below, if you're getting more than one random number it'd be more efficient to reuse the Random object rather than recreating it all the time:

    Random rand = new Random();
    boolean val = rand.nextInt(25)==0;
    

    ..

    boolean val2 = rand.nextInt(25)==0;
    
    0 讨论(0)
  • 2020-12-04 22:16

    Maybe you can implement this with generating random numbers.

      Random rn = new Random();
        double d = rn.nextDouble();     // random value in range 0.0 - 1.0
        if(d<=0.04){
        doSomeThing();
        }
    
    0 讨论(0)
  • 2020-12-04 22:21

    Generally you use a random number generator. Most of those return a number in the interval [0,1] so you would then check whether that number is <= 0.04 or not.

    if( new Random().nextDouble() <= 0.04 ) {  //you might want to cache the Random instance
       //we hit the 1/25 ( 4% ) case.
    }
    

    Or

    if( Math.random() <= 0.04 ) {
      //we hit the 1/25 ( 4% ) case.
    }
    

    Note that there are multiple random number generators that have different properties, but for simple applications the Random class should be sufficient.

    0 讨论(0)
提交回复
热议问题