溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中隨機(jī)函數(shù)變換問題怎么解決

發(fā)布時間:2022-08-24 10:37:19 來源:億速云 閱讀:103 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Java中隨機(jī)函數(shù)變換問題怎么解決”,在日常操作中,相信很多人在Java中隨機(jī)函數(shù)變換問題怎么解決問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java中隨機(jī)函數(shù)變換問題怎么解決”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

解決的問題

問題1

Java 中 Math.random()函數(shù)是等概率返回區(qū)間[0,1)中的任意一個小數(shù)。即x < 1情況下,[0,x)中的數(shù)出現(xiàn)的的概率是x,如果我們要將x < 1情況下,[0,x)中的數(shù)出現(xiàn)的的概率調(diào)整成x^2,應(yīng)該如何做?

問題1思路

由于[0,x)的概率是x,那么調(diào)用兩次Math.random(),如果較大的那個值也要在[0,x)區(qū)間內(nèi),那么兩次調(diào)用都必須在[0,x)區(qū)間內(nèi)(因為任意一次在[x,1)都會導(dǎo)致返回值不在[0,x)上),即概率是x^2,代碼如下

package snippet;

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

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

package snippet;

public class Code_0004_RandToPow2 {
    // 將`[0,x)`中的數(shù)出現(xiàn)的的概率調(diào)整成`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));
    }
}

打印的結(jié)果如下

0.0288603
0.028900000000000006

接近目標(biāo)要求。

問題2

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

思路

由于目標(biāo)是求[1,7]等概率返回一個,如果我們能加工得到一個x()函數(shù),這個函數(shù)是等概率返回[0,6]范圍內(nèi)的任意一個數(shù),那么目標(biāo)函數(shù)g()只需要調(diào)用這個x()函數(shù)再加上1,即是g()函數(shù)要求

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

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

// 通過[0,5]等概率返回的隨機(jī)函數(shù)f()
// 加工出等概率得到0和1
// 1,2,3,4,5 五個數(shù)
// 得到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 的隨機(jī)函數(shù) m(), 我們可以很方便的生成[0,6]隨機(jī)等概率返回一個數(shù)的方法,因為[0,6]需要三個二進(jìn)制數(shù)表示,那么我們可以調(diào)用三次m()函數(shù),可以等概率得到[0,7]范圍內(nèi)任意一個數(shù),我們可以在得到 7 的時候,重試上述過程,只有結(jié)果在[0,6]才返回,這樣就加工出了x()函數(shù)。

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

最后,目標(biāo)函數(shù)f()通過如下方式

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

即可得到。完整代碼如下

package snippet;

public class Code_0005_Rand5ToRand7 {

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

    // 通過[0,5]等概率返回的隨機(jī)函數(shù)f()
// 加工出等概率得到0和1
// 1,2,3,4,5 五個數(shù)
// 得到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

Java中隨機(jī)函數(shù)變換問題怎么解決

和問題2思路一致,核心都是要先實現(xiàn)一個等概率返回 0 和 1 的隨機(jī)函數(shù)m()。,然后看目標(biāo)函數(shù)區(qū)間需要幾個二進(jìn)制位,來決定調(diào)用幾次m()函數(shù),不贅述,完整代碼見

/**
 * 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 等概率返回的隨機(jī)函數(shù)
    public int m() {
        int i = rand7();
        while (i == 7) {
            i = rand7();
        }
        return (i == 1 || i == 2 || i == 3) ? 0 : 1;
    }
}

問題4

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

思路,

調(diào)用兩次f()函數(shù),可以得到如下情況

0 0
1 1
0 1
1 0

當(dāng)兩次都是0,或者兩次都是1的時候,舍棄,雖然 0 和 1 的概率不一樣,但是

0 1
1 0

概率一定一樣

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

完整代碼如下

package snippet;

// 不等概率隨機(jī)函數(shù)變成等概率隨機(jī)函數(shù)
public class Code_0005_EqualProbabilityRandom {

    // 不等概率函數(shù),
    // 內(nèi)部內(nèi)容不可見
    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;
    }

}

到此,關(guān)于“Java中隨機(jī)函數(shù)變換問題怎么解決”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI