溫馨提示×

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

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

Java隨機(jī)函數(shù)變換問題如何解決

發(fā)布時(shí)間:2022-08-24 16:01:21 來源:億速云 閱讀:146 作者:iii 欄目:編程語(yǔ)言

今天小編給大家分享一下Java隨機(jī)函數(shù)變換問題如何解決的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

解決的問題

問題1

Java 中 Math.random()函數(shù)是等概率返回區(qū)間[0,1)中的任意一個(gè)小數(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(),如果較大的那個(gè)值也要在[0,x)區(qū)間內(nèi),那么兩次調(diào)用都必須在[0,x)區(qū)間內(nèi)(因?yàn)槿我庖淮卧?code>[x,1)都會(huì)導(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());
    }
}

我們可以通過如下測(cè)試代碼來驗(yàn)證問題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());
    }

    // 測(cè)試用例
    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è)我們有一個(gè)隨機(jī)函數(shù)f(),這個(gè)函數(shù)可以等概率返回[1,5]中的一個(gè)數(shù),如何只利用f()函數(shù)而不引入其他隨機(jī)函數(shù),得到一個(gè)等概率返回[1,7]中任意一個(gè)數(shù)的函數(shù)g()。

思路

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

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

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

// 通過[0,5]等概率返回的隨機(jī)函數(shù)f()
// 加工出等概率得到0和1
// 1,2,3,4,5 五個(gè)數(shù)
// 得到1,2的時(shí)候,返回0
// 得到4,5的時(shí)候,返回1
// 得到3的時(shí)候,棄而不用,再次嘗試
    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ī)等概率返回一個(gè)數(shù)的方法,因?yàn)?code>[0,6]需要三個(gè)二進(jìn)制數(shù)表示,那么我們可以調(diào)用三次m()函數(shù),可以等概率得到[0,7]范圍內(nèi)任意一個(gè)數(shù),我們可以在得到 7 的時(shí)候,重試上述過程,只有結(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 五個(gè)數(shù)
// 得到1,2的時(shí)候,返回0
// 得到4,5的時(shí)候,返回1
// 得到3的時(shí)候,棄而不用,再次嘗試
    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思路一致,核心都是要先實(shí)現(xiàn)一個(gè)等概率返回 0 和 1 的隨機(jī)函數(shù)m()。,然后看目標(biāo)函數(shù)區(qū)間需要幾個(gè)二進(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

有一個(gè)函數(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的時(shí)候,舍棄,雖然 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)部?jī)?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;
    }

}

以上就是“Java隨機(jī)函數(shù)變換問題如何解決”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

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

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

AI