this is the java version of logic
import java.util.Random;
public class MengTeHuoEr {
public static void main(String[] args) {
boolean[] a = new boolean[3];//
Random random = new Random();
int N = 10000;
int count = 0;
for(int i = 0; i < N; iPP){//N
for(int j = 0; j < 3; jPP)
a[j] = false;
int indexOfCar = random.nextInt(3);//
a[indexOfCar] = true;
int indexOfChoose = random.nextInt(3);//
int indexOfOpen;//
for(indexOfOpen = 0; indexOfOpen < 3; indexOfOpenPP){
if(indexOfOpen != indexOfCar && indexOfOpen != indexOfChoose)
break;
}
int indexOfChange;//
for(indexOfChange = 0; indexOfChange < 3; indexOfChangePP){
if(indexOfChange != indexOfOpen && indexOfChange != indexOfChoose)
break;
}
indexOfChoose = indexOfChange;
if(a[indexOfChoose] == true)
countPP;
}
System.out.println(count*1.0/N);
}
}
for more information, please see
from Baidu.