サイコロ #7

次に、#2のルールにしたがって、同じく一億回試行してみる。
一億回は試行回数であって、ダイスを振る回数はそれ以上になる。

dice2.c
#include <stdio.h>
#include <math.h>
#include "dSFMT.h"

#define MPT 5 /* max point per a throw */
#define M (MPT * 2 + 1) /* size of counter array */

int main(void) {
    const int seed = 13579;
    const unsigned int N = 100000000;
    dsfmt_t dsfmt;
    unsigned int i, x, c[M], s;
    double e;

    for (i = 0; i < M; i++) c[i] = 0;
    dsfmt_init_gen_rand(&dsfmt, seed);
    for (i = 0; i < N; i++) {
        x = dsfmt_genrand_close_open(&dsfmt) * 6;
        if (x == MPT) x += dsfmt_genrand_close_open(&dsfmt) * 6;
        c[x]++;
    }
    s = 0;
    for (i = 0; i < M; i++) {
        printf("%u ", c[i]);
        s += c[i];
    }
    printf("%u\n", s);
    e = 0;
    for (i = 0; i < M; i++) {
        double d = (double)c[i] / s;
        printf("%f ", -log(d) / log(6));
        e += d * i;
    }
    putchar('\n');
    printf("%f\n", e);
    return 0;
}

実行すると、

$ ./dice2
16664406 16671832 16666239 16668989 16664977 2780420 2779083 2777424 2777259 2774317 2775054 100000000
1.000076 0.999827 1.000014 0.999922 1.000057 1.999469 1.999738 2.000071 2.000104 2.000696 2.000548
2.916272

結果の一行目と三行目はdice1と同じであり、得点0から10までの回数と全試行での平均得点である。
二行目は各得点が発生した回数の全試行数に対する比率をdとして-log_6dを表示したものである。
#2で示したように、点数0から4までが6^{-1}、5から10までが6^{-2}の割合で得られており、
その平均は105/36=約2.9167に近い。