A是一个随机函数,结果只有1或者0,出现1的概率为p,出现0的概率是1-p,现在需要构造一个B函数,B只能调用A来生成随机数,要求1和0的概率都是0.5。大家有什么好的想法没

解决方案 »

  1.   

    假设P为0.1
    A运行10次的数加起来得到sum
    in r;
    if(sum>=1){
    r=1;
    }else{
    r=0;
    }
    感觉不太对 关注下
      

  2.   

    B函数先 ROLL 出(1 - 100p)中随机的一个数,
    出现 数大于 100p-50 的话 (概率为0.5p),
    调用A的函数,
    如果返回为1,则返回1
    如果返回为0或者ROLL的数小于100p-50 的话,则返回0;貌似想的复杂了点。
      

  3.   

    3F+++++  public static int rand() {
        int x;
        int y;
        do {
          x = randP();
          y = randP();
        } while (x == y);
        return x;
      }
      

  4.   

    直接上代码public class Test {
    static double p = 0.15; //概率 static int A() {
    int num = (int) (Math.random() * 100);
    if (num < p * 100) {
    return 0;
    }
    return 1;
    } /*
     * 调用2次A(),将两次的值异或就可以了。
     * 因为第1次出现0的概率是p,1的概率是1-p,
     * 所以要将第2次的结果取反,这样第2次出现0的概率是1-p,1的概率是p,
     * 遇到其他情况就重新取值,直到2次结果异或为false
     * 无论两次出现的值是0,0还是1,1,概率都是p(1-p)
     */
    static int B() {
    boolean flag_0, flag_1;
    do {
    flag_0 = (A() == 0) ? true : false;
    flag_1 = (A() == 1) ? true : false;
    } while (flag_0 ^ flag_1);
    if (flag_0)
    return 0;
    return 1;
    }

    public static void main(String[] args) {
    int count_0 = 0;
    int count_1 = 0;
    /*测试1000000次*/
    for (int i = 0; i < 1000000; i++) {
    if (B() == 0)
    count_0++;
    else
    count_1++;
    }
    System.out.println("count_0 = " + count_0 + " count_1 = " + count_1);
    }
    }
      

  5.   


    public class TestABRandom {
        public static Integer A(double p) { // 假如1的概率p : 0.00001 --->>>1.0000
    if (Math.random() < p)
        return 1;
    else
        return 0;
        }    public static int B(double p) {// 用9楼代码做测试
    int x;
    int y;
    do {
        x = A(p);
        y = A(p);
    } while (x == y);
    return x;    }    public static void main(String[] args) {

    for (double p = 0.05; p < 1; p+=0.1) {//p=0.05 0.15 ....0.95
        int i = 0;
        int j = 0;
        for (int k = 0; k < 100000; k++) {// 产生100000个随机数,测试 A方法是否正确
    if (A(p) == 1)
        i++;
    else
        j++;
        }
        System.out.println("A  :" + i + "  " + j);// 在p=0.25 时 ,A的测试数据 :A  :25004  74996

        i = 0;
        j = 0;
        for (int k = 0; k < 100000; k++) {// 产生100000个随机数,测试 B方法是否正确
    if (B(p) == 1)
        i++;
    else
        j++;
        }
        System.out.println("B  :" +i + "  " + j);//在p=0.25 时 ,B的测试数据 :B  :50213  49787
    }
        }
    }/*
    A  :4948  95052     
    B  :50275  49725
    A  :15152  84848
    B  :50035  49965
    A  :25004  74996
    B  :50213  49787
    A  :35072  64928
    B  :49884  50116
    A  :45241  54759
    B  :50166  49834
    A  :54778  45222
    B  :49886  50114
    A  :64863  35137
    B  :49575  50425
    A  :74857  25143
    B  :50230  49770
    A  :84969  15031
    B  :50056  49944
    A  :95100  4900
    B  :50139  49861
    */A  :4948  95052   p = 0.05
    B  :50275  49725
    A  :15152  84848  p = 0.15
    B  :50035  49965
    A  :25004  74996  p = 0.25
    B  :50213  49787
    A  :35072  64928  
    B  :49884  50116
    A  :45241  54759
    B  :50166  49834
    A  :54778  45222
    B  :49886  50114
    A  :64863  35137
    B  :49575  50425
    A  :74857  25143
    B  :50230  49770
    A  :84969  15031
    B  :50056  49944
    A  :95100  4900   p = 0.95
    B  :50139  49861