Skip to content

Instantly share code, notes, and snippets.

@kimoto
Created June 22, 2011 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimoto/1039540 to your computer and use it in GitHub Desktop.
Save kimoto/1039540 to your computer and use it in GitHub Desktop.
中心極限定理のテスト(バグ修正版)
/*
サイコロを二回ふり、その出目の合計値を求めます。
そして上記動作を100万件実行したときに、どの目が多く出ているか確認するためのプログラムです
ちなみに、C言語のrand関数は合同線形法なので、一様乱数を出力することを確認済みです
出力結果例
$ g++ central_limit_theorem.cc
$ ./a.out
[00] 00000000
[01] 00000000
[02] 00027631
[03] 00055724
[04] 00083168
[05] 00111503
[06] 00138848
[07] 00166518
[08] 00139442
[09] 00110804
[10] 00083649
[11] 00055318
[12] 00027395
[13] 00000000
[14] 00000000
[15] 00000000
[16] 00000000
[17] 00000000
[18] 00000000
[19] 00000000
[20] 00000000
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// 1からnまでの整数をランダムに返す
int r(int n)
{
return (rand() % n) + 1;
}
// 1 - 6の値をランダムで返す
int dice()
{
return r(6);
}
// 2つのサイコロを振った結果を返す
int double_dice()
{
return dice() + dice();
}
#define N 20
int main(int argc, char const* argv[])
{
int *ibuf = new int[N+1]; // statistics
memset(ibuf, 0, sizeof(int) * (N + 1)); // zero clear
srand(time(NULL));
for(int i=0; i<1000000; i++){
ibuf[ double_dice() ]++;
}
for(int i=0; i<N+1; i++){
printf("[%02d] %08d\n", i, ibuf[i]);
}
delete [] ibuf;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment