///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Program file name: Percolate.java                                     //
//                                                                       //
// © Tao Pang 2006                                                       //
//                                                                       //
// Last modified: January 18, 2006                                       //
//                                                                       //
// (1) This Java program is part of the book, "An Introduction to        //
//     Computational Physics, 2nd Edition," written by Tao Pang and      //
//     published by Cambridge University Press on January 19, 2006.      //
//                                                                       //
// (2) No warranties, express or implied, are made for this program.     //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

// An example of creating a 2-dimensional percolation lattice.

import java.lang.*;
import java.util.Random;
public class Percolate {
  public static void main(String argv[]) {
    int n = 10;
    Random r = new Random();
    boolean o[][] = new boolean[n][n];
    o = lattice(r.nextDouble(), n);
    for (int i=0; i<n; ++i) {
      for (int j=0; j<n; ++j) {
        if(o[i][j]) System.out.print(1);
        else System.out.print(0);
      }
      System.out.println();
    }
  }

// Method to create a 2-dimensional percolation lattice.

  public static boolean[][] lattice(double p, int n) {
    Random r = new Random();
    boolean y[][] = new boolean[n][n];
    for (int i=0; i<n; ++i) {
      for (int j=0; j<n; ++j) {
        if (p > r.nextDouble()) y[i][j] = true;
        else y[i][j] = false;
      }
    }
    return y;
  }
}
