示例#1
1
 public static void main(String[] args) {
   int T = Integer.valueOf(args[0]);
   Counter heads = new Counter("heads");
   Counter tails = new Counter("tails");
   for (int i = 0; i < T; i++) {
     if (StdRandom.bernoulli(0.5)) {
       heads.increment();
     } else {
       tails.increment();
     }
   }
   StdOut.println(heads);
   StdOut.println(tails);
   int d = heads.tally() - tails.tally();
   StdOut.println("delta:" + java.lang.Math.abs(d));
 }
示例#2
0
  public static void drawRandConn(int N, double p) {
    StdDraw.setCanvasSize(512, 512);
    StdDraw.setScale(-1.0, 1.0);
    StdDraw.setPenRadius(.025);

    double[][] d = new double[N][2];
    for (int i = 0; i < N; i++) {
      d[i][0] = Math.cos(2 * Math.PI * i / N);
      d[i][1] = Math.sin(2 * Math.PI * i / N);
      StdDraw.point(d[i][0], d[i][1]);
    }

    StdDraw.setPenRadius();
    StdDraw.setPenColor(StdDraw.GRAY);

    for (int i = 0; i < N - 1; i++)
      for (int j = i + 1; j < N; j++)
        if (StdRandom.bernoulli(p)) StdDraw.line(d[i][0], d[i][1], d[j][0], d[j][1]);
  }