Random boolean with weight or bias

前端 未结 8 719
鱼传尺愫
鱼传尺愫 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 23:54

        public boolean getBiasedRandom(int bias) {
          int c;
          Random t = new Random();
         // random integers in [0, 100]
          c=t.nextInt(100);
          if (c>bias){return false;
          }
          else{return true;}
          }
    

    This one is based on percentage...

提交回复
热议问题