Probability in Java
问题 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 probability? Please point me in the general direction. 回答1: 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