/* A0620.java 6章2.解答 * 1次元ランダムウォーク (歩幅一定,時間不定) * (C) H.Ishikawa 2008 */ package simulation; import java.applet.*; import java.awt.*; import java.awt.event.*; import window.Window; public class A0620 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 = 400; int WIDTH = 640; double P0 = 0.1; /* 確率0 */ double P1 = 0.5; /* 確率1 */ long t; long x = 0; long T_END = 10000; /* 終りの時刻 */ /*グラフィックの準備*/ w.setWindow(0, 0.0,-100.0,T_END,100.0, SPACE,HIGHT-SPACE,WIDTH-SPACE,SPACE); w.axis(0, "t", T_END/10, "x", 10, g); w.moveTo(0, 0.0, 0.0, g); /*メイン*/ for (t = 0; t < T_END; t ++) { if (P0 > Math.random()) { /* 小さい確率のベルヌーイ試行 */ if (P1 > Math.random()) { x = x + 1; } else { x = x - 1; } } g.setColor(Color.green); w.lineTo(0, (double)(t), (double)(x), g); stop(); } } }