Java: do something x percent of the time

前端 未结 8 770
再見小時候
再見小時候 2020-12-09 17:04

I need a few lines of Java code that run a command x percent of the time at random.

psuedocode:

boolean x = true 10% of cases.

if(x){
  System.out.p         


        
8条回答
  •  有刺的猬
    2020-12-09 17:27

    You have to define "time" first, since 10% is a relative measure...

    E.g. x is true every 5 seconds.

    Or you could use a random number generator that samples uniformly from 1 to 10 and always do something if he samples a "1".

提交回复
热议问题