如何计算一定的成功几率的结果
如何计算一定的成功几率的结果
经常遇到比如50%的成功概率,求某次到底成功了没有,下面是我的实现方法:
/** * 以rand的概率返回true * rand范围是0.01%-99.99%。即0.0001-0.9999 */public static boolean getTrueFalse(float r){int v = (int) (r * 10000);if(v < 0 || v > 10000)throw new RuntimeException("out of range");rand.setSeed(System.nanoTime()+v);int r1 = rand.nextInt(10000) + v;return r1 - 10000 >=0 ?true:false; }