正規分布データを生成する

なんとなく、正規分布データを作成できるようになっておく。
単純に乱数を12回足してるだけ。

public class NormalDist {
    public static void main(String[] args){
        //とりあえず分布を作成する
        List<Double> l = new ArrayList<Double>();
        for(int i = 0; i < 1000; ++i){
            double d = 0;
            for(int j = 0; j < 12; ++j){
                d += Math.random();
            }
            l.add(d);
        }
        //集計する
        double total = 0;
        Map<Integer, Integer> m = new HashMap<Integer, Integer>();
        for(double d : l){
            int c = (int)(d * 5);//60領域に分割
            if(!m.containsKey(c)){
                m.put(c, 0);
            }
            m.put(c, m.get(c) + 1);
            total += d;
        }
        double ave = total / l.size();//平均
        //分散を求める
        double std = 0;
        for(double d : l){
            std += (d - ave) * (d - ave);
        }
        std /= l.size();
        //グラフを作成
        BufferedImage img = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
        Graphics g = img.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 400, 300);
        g.setColor(Color.RED);
        for(Map.Entry<Integer, Integer> elem : m.entrySet()){
            int x = elem.getKey();
            int c = elem.getValue();
            g.fillRect(x * 5 + 10, 250 - c * 2, 3, c * 2);
        }
        //正規分布を作成
        g.setColor(Color.BLUE);
        Path2D path = new Path2D.Double();
        path.moveTo(10, 250);
        for(int i = 0; i < 60; ++i){
            double x = i / 5. - ave;
            double y = Math.exp( - x * x / 2 / std) / (Math.sqrt(Math.PI * 2 * std));
            path.lineTo(i * 5 + 10, 250 - y * 330);
        }
        ((Graphics2D)g).draw(path);
        //表示する
        JFrame f = new JFrame("正規分布");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(400, 300);
        JLabel lbl = new JLabel(new ImageIcon(img));
        f.add(lbl);
        f.setVisible(true);
    }
}