// kadai2-2.cpp
// ex) plot "???.txt" w boxes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//ランダムウォークのステップ数
const int STEPS = 1000;
const int RANDOMS = 1000; //生成する乱数の個数
const int LOWER_LIMIT = 0; //ヒストグラムの下限と
const int UPPER_LIMIT = 10; //上限
const int DIVISION = 10; //分割数(1の幅をDIVISION等分)
const int HIST_MAX = (UPPER_LIMIT - LOWER_LIMIT) * DIVISION + 1;
//0以上1未満の一様乱数を生成する
double frand()
{
return rand() / (RAND_MAX+1.0);
}
int main()
{
double y;
int i, step, h;
//乱数を初期化する
srand((unsigned int)time(NULL));
//頻度を記録する配列、0で初期化
int hist[HIST_MAX] = {0};
for (i = 0; i < RANDOMS; i++) {
//STEPSステップのランダムウォークを実施する
y = 1.0;
for (step = 0; step < STEPS; step++) {
y = y * (0.9 + 0.2 * frand());
}
h = (int)(y * DIVISION);
if (h < HIST_MAX) {
hist[h]++;
}
}
for (h = 0; h < HIST_MAX; h++) {
printf("%4.1f %d\n", h / (double)DIVISION, hist[h]);
}
return 0;
}
Ly8ga2FkYWkyLTIuY3BwCi8vIGV4KSBwbG90ICI/Pz8udHh0IiB3IGJveGVzCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg+CiNpbmNsdWRlIDx0aW1lLmg+CgovL+ODqeODs+ODgOODoOOCpuOCqeODvOOCr+OBruOCueODhuODg+ODl+aVsApjb25zdCBpbnQgU1RFUFMgPSAxMDAwOwoKY29uc3QgaW50IFJBTkRPTVMgPSAxMDAwOyAvL+eUn+aIkOOBmeOCi+S5seaVsOOBruWAi+aVsApjb25zdCBpbnQgTE9XRVJfTElNSVQgPSAwOyAvL+ODkuOCueODiOOCsOODqeODoOOBruS4i+mZkOOBqApjb25zdCBpbnQgVVBQRVJfTElNSVQgPSAxMDsgLy/kuIrpmZAKY29uc3QgaW50IERJVklTSU9OID0gMTA7IC8v5YiG5Ymy5pWw77yIMeOBruW5heOCkkRJVklTSU9O562J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