隨機函數變換示例

隨機函數變換示例

作者:Grey

原文地址:隨機函數變換示例

說明

本示例中基於 Java ,其他語言也有類似的 API

解決的問題

問題1

Java 中 Math.random()函數是等概率返回區間[0,1)中的任意一個小數。
x < 1情況下,[0,x)中的數出現的的概率是x
如果我們要將
x < 1情況下,[0,x)中的數出現的的概率調整成x^2,應該如何做?

問題1思路

由於[0,x)的概率是x,那麼調用兩次Math.random(),如果較大的那個值也要在[0,x)區間內,那麼兩次調用都必須在[0,x)區間內(因為任意一次在[x,1)都會導致返回值不在[0,x)上),即概率是x^2,代碼如下


package snippet;

public class Code_0004_RandToPow2 {
    // 將`[0,x)`中的數出現的的概率調整成`x^2`
    public static double randToPow2() {
        return Math.max(Math.random(), Math.random());
    }
}

我們可以通過如下測試代碼來驗證問題1的解法:

package snippet;

public class Code_0004_RandToPow2 {
    // 將`[0,x)`中的數出現的的概率調整成`x^2`
    public static double randToPow2() {
        return Math.max(Math.random(), Math.random());
    }

    // 測試用例
    public static void main(String[] args) {
        int count = 0;
        int testTimes = 10000000;
        double x = 0.17;
        for (int i = 0; i < testTimes; i++) {
            if (randToPow2() < x) {
                count++;
            }
        }
        System.out.println((double) count / (double) testTimes);
        System.out.println(Math.pow(x, 2));
    }
}

打印的結果如下

0.0288603
0.028900000000000006

接近目標要求。

問題2

假設我們有一個隨機函數f(),這個函數可以等概率返回[1,5]中的一個數,如何只利用f()函數而不引入其他隨機函數,得到一個等概率返回[1,7]中任意一個數的函數g()

思路

由於目標是求[1,7]等概率返回一個,如果我們能加工得到一個x()函數,這個函數是等概率返回[0,6]範圍內的任意一個數,那麼目標函數g()只需要調用這個x()函數再加上1,即是g()函數要求

public static int g() {
    return x() + 1;
}

要得到[0,6]等概率返回一個數,我們需要先得到一個0和1等概率返回的隨機函數m(),我們可以通過f()函數來得到,即

// 通過[0,5]等概率返回的隨機函數f()
// 加工出等概率得到0和1
// 1,2,3,4,5 五個數
// 得到1,2的時候,返回0
// 得到4,5的時候,返回1
// 得到3的時候,棄而不用,再次嘗試
    public static int m() {
        int ans = 0;
        do {
            ans = f();
        } while (ans == 3);
        return ans < 3 ? 0 : 1;
    }

有了等概率返回 0 和 1 的隨機函數 m(), 我們可以很方便的生成[0,6]隨機等概率返回一個數的方法,因為[0,6]需要三個二進制數表示,那麼我們可以調用三次m()函數,可以等概率得到[0,7]範圍內任意一個數,我們可以在得到 7 的時候,重試上述過程,只有結果在[0,6]才返回,這樣就加工出了x()函數。

    // 等概率返回0~6
    public static int x() {
        int ans = 0;
        do {
            ans = (m() << 2) + (m() << 1) + m();
        } while (ans == 7);
        return ans;
    }

最後,目標函數f()通過如下方式

    // 等概率返回1~7
    public static int g() {
        return x() + 1;
    }

即可得到。完整代碼如下

package snippet;

public class Code_0005_Rand5ToRand7 {

    // 此函數只能用,不能修改
    // 等概率返回1~5
    public static int f() {
        return (int) (Math.random() * 5) + 1;
    }

    // 通過[0,5]等概率返回的隨機函數f()
// 加工出等概率得到0和1
// 1,2,3,4,5 五個數
// 得到1,2的時候,返回0
// 得到4,5的時候,返回1
// 得到3的時候,棄而不用,再次嘗試
    public static int m() {
        int ans = 0;
        do {
            ans = f();
        } while (ans == 3);
        return ans < 3 ? 0 : 1;
    }

    // 等概率返回0~6
    public static int x() {
        int ans = 0;
        do {
            ans = (m() << 2) + (m() << 1) + m();
        } while (ans == 7);
        return ans;
    }

    // 等概率返回1~7
    public static int g() {
        return x() + 1;
    }

    
}

問題3

見:LeetCode 470. Implement Rand10() Using Rand7()

和問題2思路一致,核心都是要先實現一個等概率返回 0 和 1 的隨機函數m(),然後看目標函數區間需要幾個二進制位,來決定調用幾次m()函數,不贅述,完整代碼見

/**
 * The rand7() API is already defined in the parent class SolBase.
 * public int rand7();
 * @return a random integer in the range 1 to 7
 */
class Solution extends SolBase {
    public int rand10() {
        return rand(10);
    }

    public int rand(int N) {
        int bit = 1;
        int base = 2;
        while (base <= N) {
            base = 2 << bit;
            bit++;
        }
        int v = build(bit);
        while (v < 1 || v > N) {
            v = build(bit);
        }
        return v;
    }

    private int build(int bit) {
        int v = 0;
        for (int i = 0; i < bit; i++) {
            v += (m() << i);
        }
        return v;
    }

    // 核心:生成 0 和 1 等概率返回的隨機函數
    public int m() {
        int i = rand7();
        while (i == 7) {
            i = rand7();
        }
        return (i == 1 || i == 2 || i == 3) ? 0 : 1;
    }
}

問題4

有一個函數f(),不等概率(但是是固定概率)返回0和1,如何只通過f()函數,得到等概率返回 0 和 1 的隨機函數g()

思路,

調用兩次f()函數,可以得到如下情況

0 0
1 1
0 1
1 0

當兩次都是0,或者兩次都是1的時候,捨棄,雖然 0 和 1 的概率不一樣,但是

0 1
1 0

概率一定一樣

所以得到 0 1就返回 0 ,得到 1 0就返回1,即g()函數

完整代碼如下

package snippet;

// 不等概率隨機函數變成等概率隨機函數
public class Code_0005_EqualProbabilityRandom {

    // 不等概率函數,
    // 內部內容不可見
    public static int f() {
        return Math.random() < 0.8 ? 0 : 1;
    }

    // 等概率返回0和1
    public static int g() {
        int first;
        do {
            first = f(); // 0 1
        } while (first == f());
        return first;
    }

}

更多

算法和數據結構筆記