Interview Question
Country: United States
Sample code
public class BiasedProb {
public static void main(String[] args) {
BiasedProb biased = new BiasedProb();
double prob = 0.67;
int count = 1000000;
int appear = 0;
for(int i=0;i<1000000;i++){
if(biased.biased(prob)){
appear++;
}
}
System.out.println(((double)appear)/count);
}
public boolean biased(double prob){
double randVal = Math.random();
return randVal<prob;
}
}
in java Math.random() generates values between 0 to <1 with almost equal distribution over large no of iterations... since it is so the following code could work
- PeyarTheriyaa March 08, 2018