/* S0350.java * 中心極限定理 * (C) H.Ishikawa 2008 */ package simulation; import java.applet.*; import java.awt.*; import java.awt.event.*; import window.Window; public class S0350 extends Applet implements ActionListener { Button button0; public void init() { button0 = new Button(" 再実行 "); add(button0); button0.addActionListener(this); } public void actionPerformed(ActionEvent e) { String label = e.getActionCommand(); repaint(); } public void paint(Graphics g) { Window w; w = new Window(); int SPACE = 30; int HIGHT = 640; int WIDTH = 640; int NUMBER = 5000; double P = 0.5; /* 確率p */ int SIZE = 100; /* ヒストグラムの大きさ */ int j; /* forのカウンタ */ int i; /* forのカウンタ */ int u; /* xをSIZE等分したものの整数部分 */ int n = 1; /* n = 1, 2, 4, 8, 16 */ double x; /* 一様乱数n個の合計 */ double x1; /* 横軸 */ double y1; /* 縦軸 */ int f[] = new int[SIZE]; /* ヒストグラム */ /* グラフィックの準備 */ int hight = HIGHT / 5 - SPACE; w.setWindow(0, 0.0, 0.0, 16, 0.05, SPACE, hight, WIDTH - SPACE, SPACE); w.axis(0, "", 1, "n=1", 0.01, g); w.setWindow(1, 0.0, 0.0, 16, 0.05, SPACE, 2 * hight, WIDTH - SPACE, hight + SPACE); w.axis(1, "", 1, "n=2", 0.01, g); w.setWindow(2, 0.0, 0.0, 16, 0.05, SPACE, 3 * hight, WIDTH - SPACE, 2 * hight + SPACE); w.axis(2, "", 1, "n=4", 0.01, g); w.setWindow(3, 0.0, 0.0, 16, 0.05, SPACE, 4 * hight, WIDTH - SPACE, 3 * hight + SPACE); w.axis(3, "", 1, "n=8", 0.01, g); w.setWindow(4, 0.0, 0.0, 16, 0.05, SPACE, 5 * hight, WIDTH - SPACE, 4 * hight + SPACE); w.axis(4, "", 1, "n=16", 0.01, g); g.setColor(Color.green); /* メイン */ for (i = 0; i < 5; i++) { for (j = 1; j <= NUMBER; j++) { x = central(n); u = (int) (x * SIZE / n); f[u] = f[u] + 1; } /* ヒストグラムを書く */ for (u = 0; u < SIZE; u++) { x1 = (double)u / SIZE * n; y1 = (double)f[u] / NUMBER; w.line(i, x1, 0, x1, y1, g); f[u] = 0; } n = n * 2; } } public double central(int n) { int k; double x = 0.0; for (k = 0; k < n; k++) { x = x + Math.random(); } return (x); } }