0

タスク : 不公平なサイコロ (6 面) が n 回振られています。1 の確率は p1、2 の確率は p2 などです。与えられた n (n<100) に対して、セット (p1,p2,p3,p4,p5,p6) の確率と $x \in [n,600n]$ の合計の確率を求めるコンピューター プログラムを作成します。サイコロの値は x 未満です。プログラムは 5 分以上動作できません。これは私に追加のポイントを与える追加の質問ですが、これまで誰もそれを行っていません. 私のような初心者のコンピューター科学者も、このコードから学ぶことができると思います.Webでバイアスダイスに関するヘルプが0件見つかり、ルーレットのような解決策を思いついたからです。私も世界に私のやり方を見せたかったのです。

幾何学的確率と統計的確率を使用した 2 つの解決策があります。

私の質問は次のとおりです。1)このようにすると正しいですか、それともどこかで間違っていますか?2) 幾何学的確率と統計的確率のどちらがより良い答えをくれると思いますか? 私の直感では、より信頼性が高いため、幾何学的であると言えます。私のコードが私に与えているのは正しい答えだと思います-0.99以上.....通常。まったくわからないので、誰かに私の仕事をチェックしてもらいたいと思っていました。このコードを他の人と共有したかったのです。

ループを使用するとRよりもはるかに高速であるため、Javaの方が好きですが、統計用にもRコードを指定しました。それらは非常に似ているため、問題にならないことを願っています。

Java コード:

import java.util.ArrayList;


public class Statistical_prob_lisayl2_geometrical {

    public static double mean(ArrayList<Double> a) {
        double sum=0;
        int len = a.size();
        for (int i = 0; i < len; i++) {
            sum = sum + a.get(i);
        }
        return (sum/len);
    }

    public static double geom_prob(double p1,double p2,double p3,double p4,double p5,double p6){
        ArrayList<Double> prob_values = new ArrayList<Double>();

        int repeatcount = 1000000;
        int[] options = {1,2,3,4,5,6};
        int n = 50;
        double[] probabilities = {p1,p2,p3,p4,p5,p6};
        for (int i = 0 ; i < repeatcount ; i++ ) { // a lot of repeats for better statistical probability
            int sum = 0; //for each repeat, the sum is being computed
            for (int j = 0; j < n ; j++ ) { // for each repeat there is n cast of dies and we compute them here
                double probability_value=0; // the value we start looking from with probability
                double instant_probability = Math.random(); // we generate random probability for dice value
                    for (int k = 0; k < 6; k++ ) { // because we have 6 sides, we start looking at each probability like a roulette table
                        probability_value = probability_value + probabilities[k]; // we sum the probabilities for checking in which section the probability belongs to
                        if (probability_value>instant_probability) {
                            sum = sum + options[k]; // if probability belongs to certain area , lets say p3 to p4, then p3 is added to sum
                                break; // we break the loop , because it would give us false values otherwise
                        }
                    }
                }
            double length1 = (600*n)-n-(sum-n); //length of possible x values minus length of sum
            double length2 = 600*n-n;
            prob_values.add( (length1/length2) ); // geometric probability l1/l2

            }
        return mean(prob_values); // we give the mean value of a arraylist, with 1000000 numbers in it
    }
    public static double stat_prob(double p1,double p2,double p3,double p4,double p5,double p6){
        ArrayList<Double> prob_values = new ArrayList<Double>();

        int repeatcount = 1000000;
        int[] options = {1,2,3,4,5,6};
        int n = 50;
        double[] probabilities = {p1,p2,p3,p4,p5,p6};
        int count = 0;
        for (int i = 0 ; i < repeatcount ; i++ ) {
            int sum = 0;
            for (int j = 0; j < n ; j++ ) {
                double probability_value=0;
                double instant_probability = Math.random();
                    for (int k = 0; k < 6; k++ ) {
                        probability_value = probability_value + probabilities[k];
                        if (probability_value>instant_probability) {
                            sum = sum + options[k];
                                break;
                        }
                    }
                }
            int x = (int)Math.round(Math.random()*(600*n-n)+n);
            if( x>sum ) {
                count = count + 1;  
            }
        }
        double probability = (double)count/(double)repeatcount;
        return probability;
    }

    public static void main(String[] args) {
        System.out.println(stat_prob(0.1,0.1,0.1,0.1,0.3,0.3));
        System.out.println(geom_prob(0.1,0.1,0.1,0.1,0.3,0.3)); 
    }

}

R コード:

repeatcount = 100000
options = c(1,2,3,4,5,6)
n = 50
probabilities = c(1/10,1/10,1/10,1/10,3/10,3/10)

count = 0
for (i in 1:repeatcount) {
sum = 0
for (i in 1:n) {
    probability_value=0
    instant_probability = runif(1,0,1)
    for (k in 1:6){
        probability_value = probability_value + probabilities[k]
        if (probability_value>instant_probability) {
            sum = sum + options[k] 
            break
        }
        }
    }
    x = runif(1,n,600*n)
    x
    sum
    if ( x> sum ) {
        count = count + 1   
    }
}
count
probability = count/repeatcount
probability
4

1 に答える 1