Random boolean with weight or bias

前端 未结 8 705
鱼传尺愫
鱼传尺愫 2020-12-01 23:42

I need to generate some random booleans. However I need to be able to specify the probability of returning true. As a results doing:

private R         


        
8条回答
  •  既然无缘
    2020-12-02 00:07

    Random object needs to be intialized already.

    public static boolean flipRandom(double probability) {
        Validate.isBetween(probability, 0, 1, true);
        if(probability == 0)
            return false;
        if(probability == 1)
            return true;
        if(probability == 0.5)
            return random.nextBoolean();
        return random.nextDouble() < probability ? true : false;
    }
    

提交回复
热议问题